silverstripe-fulltextsearch/src/Solr/Reindex/Handlers/SolrReindexImmediateHandler...

103 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2017-04-21 02:27:01 +02:00
namespace SilverStripe\FullTextSearch\Solr\Reindex\Handlers;
use Psr\Log\LoggerInterface;
use SilverStripe\Control\Director;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\FullTextSearch\Solr\Solr;
use SilverStripe\FullTextSearch\Solr\SolrIndex;
use SilverStripe\ORM\DB;
use Symfony\Component\Process\Process;
/**
* Invokes an immediate reindex
*
* Internally batches of records will be invoked via shell tasks in the background
*/
2015-11-21 07:19:20 +01:00
class SolrReindexImmediateHandler extends SolrReindexBase
{
public function triggerReindex(LoggerInterface $logger, $batchSize, $taskName, $classes = null)
{
$this->runReindex($logger, $batchSize, $taskName, $classes);
}
2015-11-21 07:19:20 +01:00
protected function processIndex(
LoggerInterface $logger,
SolrIndex $indexInstance,
$batchSize,
$taskName,
$classes = null
2015-11-21 07:19:20 +01:00
) {
parent::processIndex($logger, $indexInstance, $batchSize, $taskName, $classes);
2015-11-21 07:19:20 +01:00
// Immediate processor needs to immediately commit after each index
$indexInstance->getService()->commit();
}
2015-11-21 07:19:20 +01:00
/**
* Process a single group.
*
* Without queuedjobs, it's necessary to shell this out to a background task as this is
* very memory intensive.
*
* The sub-process will then invoke $processor->runGroup() in {@see Solr_Reindex::doReindex}
*
* @param LoggerInterface $logger
* @param SolrIndex $indexInstance Index instance
* @param array $state Variant state
* @param string $class Class to index
* @param int $groups Total groups
* @param int $group Index of group to process
* @param string $taskName Name of task script to run
*/
protected function processGroup(
LoggerInterface $logger,
SolrIndex $indexInstance,
$state,
$class,
$groups,
$group,
$taskName
2015-11-21 07:19:20 +01:00
) {
// Build state
$statevar = json_encode($state);
if (strpos(PHP_OS, "WIN") !== false) {
$statevar = '"'.str_replace('"', '\\"', $statevar).'"';
} else {
$statevar = "'".$statevar."'";
}
2015-11-21 07:19:20 +01:00
// Build script
$indexName = $indexInstance->getIndexName();
$indexClass = get_class($indexInstance);
$indexClassEscaped = addslashes($indexClass);
$class = addslashes($class);
$frameworkPath = ModuleLoader::getModule('silverstripe/framework')->getPath();
$scriptPath = sprintf("%s%scli-script.php", $frameworkPath, DIRECTORY_SEPARATOR);
2015-11-21 07:19:20 +01:00
$scriptTask = "php {$scriptPath} dev/tasks/{$taskName}";
$cmd = "{$scriptTask} index={$indexClassEscaped} class={$class} group={$group} groups={$groups} variantstate={$statevar}";
$cmd .= " verbose=1";
2015-11-21 07:19:20 +01:00
$logger->info("Running '$cmd'");
2015-11-21 07:19:20 +01:00
// Execute script via shell
$process = new Process($cmd);
$process->inheritEnvironmentVariables();
$process->run();
$res = $process->getOutput();
2015-11-21 07:19:20 +01:00
if ($logger) {
$logger->info(preg_replace('/\r\n|\n/', '$0 ', $res));
}
2015-11-21 07:19:20 +01:00
// If we're in dev mode, commit more often for fun and profit
if (Director::isDev()) {
Solr::service($indexClass)->commit();
2015-11-21 07:19:20 +01:00
}
2015-11-21 07:19:20 +01:00
// This will slow down things a tiny bit, but it is done so that we don't timeout to the database during a reindex
DB::query('SELECT 1');
}
}