diff --git a/src/Insulin/Console/Command/CronCommand.php b/src/Insulin/Console/Command/CronCommand.php new file mode 100644 index 0000000..7e91806 --- /dev/null +++ b/src/Insulin/Console/Command/CronCommand.php @@ -0,0 +1,111 @@ +setName('cron:run') + ->setDefinition(array( + new InputArgument('jobId', InputArgument::IS_ARRAY, 'The specific scheduler job id to run.'), + new InputOption('force', 'f', InputOption::VALUE_OPTIONAL, 'Force the cron job run.', false), + )) + ->setDescription('Run all active cron jobs or all jobs given.') + ->setHelp( + <<%command.name% command runs all active cron jobs: + + %command.full_name% + +You can also run only some specific job or list of jobs by providing the ids: + + %command.full_name% 7542f4f8-7d15-dd03-d6a1-52af6cfee427 804d64aa-f22f-c185-48c1-52af6c580622 + +It's also possible to force the cron to run (useful to ignore status and last run time): + %command.full_name% --force +EOF + ); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + /* @var $kernel \Insulin\Console\KernelInterface */ + $kernel = $this->getApplication()->getKernel(); + + if ($kernel->getBootedLevel() < $kernel::BOOT_SUGAR_FULL) { + // FIXME change this to a common exception to be used by all commands + throw new \Exception('Cannot execute command, no valid SugarCRM instance found.'); + } + + $sugar = $kernel->get('sugar'); + + // TODO move this to Sugar proxy to abstract per version (this is only for 7.x with Job Queue changes + $jobIds = $input->getArgument('jobId'); + + require_once 'modules/Schedulers/Scheduler.php'; + $s = new \Scheduler(); + $jobs = $s->get_full_list('', "schedulers.status='Active'"); + + if (!empty($jobIds)) { + $jobs = array_filter($jobs, function ($job) use ($jobIds) { + return in_array($job->id, $jobIds); + }); + } + + if (empty($jobs)) { + throw new RuntimeException('No Scheduler jobs to run.'); + } + + foreach ($jobs as $focus) { + if ($input->getOption('force') || $focus->fireQualified()) { + $job = $focus->createJob(); + if (!$job->runJob()) { + // TODO provide better exceptions for failed jobs + throw new \RuntimeException( + sprintf("Cron failed for job id '%s'.", $focus->id) + ); + } + $output->writeln( + sprintf("Job '%s' ran successful.", $job->scheduler_id) + ); + } + } + + $output->writeln('Cron run successfully.'); + } +}