2015-07-16 08:18:04 +02:00
|
|
|
<?php
|
2017-04-21 03:18:37 +02:00
|
|
|
|
2017-04-21 02:27:01 +02:00
|
|
|
namespace SilverStripe\FullTextSearch\Solr\Reindex\Handlers;
|
2017-04-21 03:18:37 +02:00
|
|
|
|
2015-07-16 08:18:04 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-04-22 20:21:06 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2018-09-13 01:22:18 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Environment;
|
2017-11-15 22:17:49 +01:00
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
2017-04-22 20:21:06 +02:00
|
|
|
use SilverStripe\FullTextSearch\Solr\Solr;
|
2017-04-21 03:18:37 +02:00
|
|
|
use SilverStripe\FullTextSearch\Solr\SolrIndex;
|
2017-04-22 20:21:06 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2018-02-16 00:05:46 +01:00
|
|
|
use Symfony\Component\Process\Process;
|
2018-10-30 08:44:51 +01:00
|
|
|
use SilverStripe\Core\Config\Configurable;
|
2015-07-16 08:18:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
2018-10-30 08:44:51 +01:00
|
|
|
|
|
|
|
use Configurable;
|
|
|
|
|
2018-09-13 01:22:18 +02:00
|
|
|
/**
|
|
|
|
* Path to the php binary
|
|
|
|
* @config
|
|
|
|
* @var null|string
|
|
|
|
*/
|
|
|
|
private static $php_bin = 'php';
|
|
|
|
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function triggerReindex(LoggerInterface $logger, $batchSize, $taskName, $classes = null)
|
|
|
|
{
|
|
|
|
$this->runReindex($logger, $batchSize, $taskName, $classes);
|
|
|
|
}
|
2015-07-16 08:18:04 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
protected function processIndex(
|
2017-11-14 21:48:52 +01:00
|
|
|
LoggerInterface $logger,
|
|
|
|
SolrIndex $indexInstance,
|
|
|
|
$batchSize,
|
|
|
|
$taskName,
|
|
|
|
$classes = null
|
2015-11-21 07:19:20 +01:00
|
|
|
) {
|
|
|
|
parent::processIndex($logger, $indexInstance, $batchSize, $taskName, $classes);
|
2015-07-16 08:18:04 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Immediate processor needs to immediately commit after each index
|
|
|
|
$indexInstance->getService()->commit();
|
|
|
|
}
|
2015-07-16 08:18:04 +02:00
|
|
|
|
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(
|
2017-11-14 21:48:52 +01:00
|
|
|
LoggerInterface $logger,
|
|
|
|
SolrIndex $indexInstance,
|
|
|
|
$state,
|
|
|
|
$class,
|
|
|
|
$groups,
|
|
|
|
$group,
|
|
|
|
$taskName
|
2015-11-21 07:19:20 +01:00
|
|
|
) {
|
2018-02-23 04:46:44 +01:00
|
|
|
$indexClass = get_class($indexInstance);
|
2018-09-13 01:22:18 +02:00
|
|
|
|
2018-02-23 04:46:44 +01:00
|
|
|
// Build script parameters
|
|
|
|
$indexClassEscaped = $indexClass;
|
2015-11-21 07:19:20 +01:00
|
|
|
$statevar = json_encode($state);
|
2018-02-23 04:46:44 +01:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
if (strpos(PHP_OS, "WIN") !== false) {
|
2018-06-27 06:50:54 +02:00
|
|
|
$statevar = '"' . str_replace('"', '\\"', $statevar) . '"';
|
2015-11-21 07:19:20 +01:00
|
|
|
} else {
|
2018-06-27 06:50:54 +02:00
|
|
|
$statevar = "'" . $statevar . "'";
|
2018-02-23 04:46:44 +01:00
|
|
|
$class = addslashes($class);
|
|
|
|
$indexClassEscaped = addslashes($indexClass);
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
2015-07-16 08:18:04 +02:00
|
|
|
|
2018-09-13 01:22:18 +02:00
|
|
|
$php = Environment::getEnv('SS_PHP_BIN') ?: Config::inst()->get(static::class, 'php_bin');
|
|
|
|
|
2018-02-23 04:46:44 +01:00
|
|
|
// Build script line
|
2017-11-15 22:17:49 +01:00
|
|
|
$frameworkPath = ModuleLoader::getModule('silverstripe/framework')->getPath();
|
|
|
|
$scriptPath = sprintf("%s%scli-script.php", $frameworkPath, DIRECTORY_SEPARATOR);
|
2018-09-13 01:22:18 +02:00
|
|
|
$scriptTask = "{$php} {$scriptPath} dev/tasks/{$taskName}";
|
2017-05-19 01:23:12 +02:00
|
|
|
|
|
|
|
$cmd = "{$scriptTask} index={$indexClassEscaped} class={$class} group={$group} groups={$groups} variantstate={$statevar}";
|
2018-02-16 00:05:46 +01:00
|
|
|
$cmd .= " verbose=1";
|
2015-11-21 07:19:20 +01:00
|
|
|
$logger->info("Running '$cmd'");
|
2015-07-16 08:18:04 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Execute script via shell
|
2018-02-16 00:05:46 +01:00
|
|
|
$process = new Process($cmd);
|
2018-10-25 01:27:49 +02:00
|
|
|
|
2018-10-25 10:59:20 +02:00
|
|
|
// Set timeout from config. Process default is 60 seconds
|
|
|
|
$process->setTimeout($this->config()->get('process_timeout'));
|
2018-10-25 01:27:49 +02:00
|
|
|
|
2018-02-16 00:05:46 +01:00
|
|
|
$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-07-16 08:18:04 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// If we're in dev mode, commit more often for fun and profit
|
|
|
|
if (Director::isDev()) {
|
2016-03-22 10:13:46 +01:00
|
|
|
Solr::service($indexClass)->commit();
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
2015-07-16 08:18:04 +02: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');
|
|
|
|
}
|
2015-07-16 08:18:04 +02:00
|
|
|
}
|