diff --git a/cli/finder_indexer.php b/cli/finder_indexer.php index 5c1ed6b19121f..c844cddab1399 100644 --- a/cli/finder_indexer.php +++ b/cli/finder_indexer.php @@ -12,10 +12,30 @@ * This is a command-line script to help with management of Smart Search. * * Called with no arguments: php finder_indexer.php - * Performs an incremental update of the index. + * Performs an incremental update of the index using dynamic pausing. * - * Called with --purge: php finder_indexer.php --purge + * IMPORTANT NOTE: since Joomla version __DEPLOY_VERSION__ the default behavior of this script has changed. + * If called with no arguments, the `--pause` argument is silently applied, in order to avoid the possibility of + * stressing the server too much and making a site (or multiple sites, if on a shared environment) unresponsive. + * If a pause is unwanted, just apply `--pause=0` to the command + * + * Called with --purge php finder_indexer.php --purge * Purges and rebuilds the index (search filters are preserved). + * + * Called with --pause `php finder_indexer.php --pause` + * or --pause=x or `php finder_indexer.php --pause=x` where x = seconds. + * or --pause=division or `php finder_indexer.php --pause=division` The default divisor is 5. + * If another divisor is required, it can be set with --divisor=y, where + * y is the integer divisor + * + * This will pause for x seconds between batches, + * in order to give the server some time to catch up + * if --pause is called without an assignment, it defaults to dynamic pausing + * using the division method with a divisor of 5 + * (eg. 1 second pause for every 5 seconds of batch processing time) + * + * Called with --minproctime=x Will set the minimum processing time of batches for a pause to occur. Defaults to 1 + * */ // We are a valid entry point. @@ -91,6 +111,37 @@ class FinderCli extends JApplicationCli */ private $filters = array(); + /** + * Pausing type or defined pause time in seconds. + * One pausing type is implemented: 'division' for dynamic calculation of pauses + * + * Defaults to 'division' + * + * @var string|integer + * @since __DEPLOY_VERSION__ + */ + private $pause = 'division'; + + /** + * The divisor of the division: batch-processing time / divisor. + * This is used together with --pause=division in order to pause dynamically + * in relation to the processing time + * Defaults to 5 + * + * @var integer + * @since __DEPLOY_VERSION__ + */ + private $divisor = 5; + + /** + * Minimum processing time in seconds, in order to apply a pause + * Defaults to 1 + * + * @var integer + * @since __DEPLOY_VERSION__ + */ + private $minimumBatchProcessingTime = 1; + /** * Entry point for Smart Search CLI script * @@ -114,6 +165,20 @@ public function doExecute() $_SERVER['HTTP_HOST'] = 'domain.com'; JFactory::getApplication('site'); + $this->minimumBatchProcessingTime = $this->input->getInt('minproctime', 1); + + // Pause between batches to let the server catch a breath. The default, if not set by the user, is set in the class property `pause` + $pauseArg = $this->input->get('pause', $this->pause, 'raw'); + + if ($pauseArg === 'division') + { + $this->divisor = $this->input->getInt('divisor', $this->divisor); + } + else + { + $this->pause = (int) $pauseArg; + } + // Purge before indexing if --purge on the command line. if ($this->input->getString('purge', false)) { @@ -207,7 +272,43 @@ private function index() JEventDispatcher::getInstance()->trigger('onBuildIndex'); // Batch reporting. - $this->out(JText::sprintf('FINDER_CLI_BATCH_COMPLETE', $i + 1, round(microtime(true) - $this->qtime, 3)), true); + $this->out(JText::sprintf('FINDER_CLI_BATCH_COMPLETE', $i + 1, $processingTime = round(microtime(true) - $this->qtime, 3)), true); + + if ($this->pause !== 0) + { + // Pausing Section + $skip = !($processingTime >= $this->minimumBatchProcessingTime); + $pause = 0; + + if ($this->pause === 'division' && $this->divisor > 0) + { + if (!$skip) + { + $pause = round($processingTime / $this->divisor); + } + else + { + $pause = 1; + } + } + elseif ($this->pause > 0) + { + $pause = $this->pause; + } + + if ($pause > 0 && !$skip) + { + $this->out(JText::sprintf('FINDER_CLI_BATCH_PAUSING', $pause), true); + sleep($pause); + $this->out(JText::_('FINDER_CLI_BATCH_CONTINUING')); + } + + if ($skip) + { + $this->out(JText::sprintf('FINDER_CLI_SKIPPING_PAUSE_LOW_BATCH_PROCESSING_TIME', $processingTime, $this->minimumBatchProcessingTime), true); + } + // End of Pausing Section + } } } catch (Exception $e) @@ -331,7 +432,7 @@ private function getFilters() $this->out(JText::_('FINDER_CLI_SAVE_FILTERS')); // Get the taxonomy ids used by the filters. - $db = JFactory::getDbo(); + $db = JFactory::getDbo(); $query = $db->getQuery(true); $query ->select('filter_id, title, data') diff --git a/language/en-GB/en-GB.finder_cli.ini b/language/en-GB/en-GB.finder_cli.ini index 94494edce7e5d..62c1421862d11 100644 --- a/language/en-GB/en-GB.finder_cli.ini +++ b/language/en-GB/en-GB.finder_cli.ini @@ -5,6 +5,8 @@ FINDER_CLI="Smart Search INDEXER" FINDER_CLI_BATCH_COMPLETE=" * Processed batch %s in %s seconds." +FINDER_CLI_BATCH_CONTINUING=" * Continuing processing of batch ..." +FINDER_CLI_BATCH_PAUSING=" * Pausing processing for %s seconds ..." FINDER_CLI_FILTER_RESTORE_WARNING="Warning: Did not find taxonomy %s/%s in filter %s" FINDER_CLI_INDEX_PURGE="Clear index" FINDER_CLI_INDEX_PURGE_FAILED="- index clear failed." @@ -17,5 +19,6 @@ FINDER_CLI_SAVE_FILTER_COMPLETED="- number of saved filters: %s" FINDER_CLI_SAVE_FILTERS="Saving filters" FINDER_CLI_SETTING_UP_PLUGINS="Setting up Smart Search plugins" FINDER_CLI_SETUP_ITEMS="Setup %s items in %s seconds." +FINDER_CLI_SKIPPING_PAUSE_LOW_BATCH_PROCESSING_TIME=" * Skipping pause, as previous batch had a very low processing time (%ss < %ss)" FINDER_CLI_STARTING_INDEXER="Starting Indexer"