Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sugar CLI command to run index processing without waiting for the scheduler - SUPERSEDED, LOOK INTO ./bin/sugarcrm search:silent_reindex and ./bin/sugarcrm search:silent_reindex_mp
#!/usr/local/bin/php
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// set permissions with:
// chmod +x ./filename.php
// run in the background, logging on a file with :
// ./filename.php 2>&1 > /var/log/mylog.log &
$directory = '/var/www/html/sugar';
$seconds_delay = 5;
$command = './bin/sugarcrm elastic:process_records';
$log_string = 'sugar elastic processing';
$pause_file = 'elastic_pause';
while (true) {
if (is_dir($directory) && !file_exists($directory . '/' . $pause_file)) {
echo '[' . date('Y-m-d H:i:s') . '] ' . $log_string . ' start' . PHP_EOL;
$output = array();
exec('cd ' . $directory . ' && ' . $command . ' && cd /', $output);
if (!empty($output)) {
echo implode($output, PHP_EOL) . PHP_EOL;
}
echo '[' . date('Y-m-d H:i:s') . '] ' . $log_string . ' end' . PHP_EOL;
if (file_exists($directory . '/' . $pause_file)) {
echo 'Pausing ' . $log_string . ' processing now. To unpause, remove ' . $directory . $pause_file . PHP_EOL;
}
}
// delay
sleep($seconds_delay);
}
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-12-15 on Sugar 7.9.2.0
// Sugar CLI command to run index processing without waiting for the scheduler
//
// Trigger reindex with: ./bin/sugarcrm search:reindex
// Process records by running either as a background process or with a cron: ./bin/sugarcrm elastic:process_records
//
// file: custom/src/Console/Command/Elasticsearch/ProcessQueueCommand.php
namespace Sugarcrm\Sugarcrm\custom\Console\Command\Elasticsearch;
use Sugarcrm\Sugarcrm\Console\CommandRegistry\Mode\InstanceModeInterface;
use Sugarcrm\Sugarcrm\SearchEngine\SearchEngine;
use Sugarcrm\Sugarcrm\SearchEngine\Engine\Elastic;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use RuntimeException;
/**
*
* ProcessQueueCommand manually, instead of leveraging the scheduler job
*
*/
class ProcessQueueCommand extends Command implements InstanceModeInterface
{
/**
* {inheritdoc}
*/
protected function configure()
{
$this
->setName('elastic:process_records')
->setDescription('Process next batch of records.')
;
}
/**
* {inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$engineInstance = SearchEngine::getInstance();
if($engineInstance->isAvailable(true)) {
if (!$engineInstance->getEngine() instanceof Elastic) {
throw new RuntimeException('Backend search engine is not Elastic');
}
$list = array();
foreach ($engineInstance->getContainer()->queueManager->getQueuedModules() as $module) {
$engineInstance->getContainer()->queueManager->consumeModuleFromQueue($module);
$list[] = $module;
}
$stopped = $this->stopElasticsearchScheduler();
if ($stopped) {
$output->writeln('Elasticsearch scheduler deactivated. Keep using CLI or reactivate');
}
if (!empty($list)) {
$message = 'Processed records for: '.implode(', ', $list);
} else {
$message = 'No record processed';
}
$output->writeln($message);
}
}
/**
* Deactivate Elasticsearch Scheduler
*
* @return bool Deactivated successfully true or false
*/
protected function stopElasticsearchScheduler()
{
$schedulerClass = \SugarAutoLoader::customClass('Sugarcrm\\Sugarcrm\\Elasticsearch\\Queue\\Scheduler');
$schedulerExec = "class::\\{$schedulerClass}";
$scheduler = \BeanFactory::newBean('Schedulers');
$sq = new \SugarQuery();
$sq->select('id');
$sq->from($scheduler)->where()->equals('job', $schedulerExec);
$result = $scheduler->fetchFromQuery($sq);
if (!empty($result)) {
$scheduler = array_shift($result);
if ($scheduler->status == 'Active') {
$scheduler->status = 'Inactive';
$scheduler->save();
return true;
}
}
return false;
}
}
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-12-15 on Sugar 7.9.2.0
// Sugar CLI command to run index processing without waiting for the scheduler
//
// file: custom/Extension/application/Ext/Console/RegisterElasticsearchProcessQueueCommand.php
$commandregistry = Sugarcrm\Sugarcrm\Console\CommandRegistry\CommandRegistry::getInstance();
$commandregistry->addCommands(array(new Sugarcrm\Sugarcrm\custom\Console\Command\Elasticsearch\ProcessQueueCommand()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.