-
Notifications
You must be signed in to change notification settings - Fork 0
[WIP] Create cron run command #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alias-mac
wants to merge
1
commit into
insulin:master
Choose a base branch
from
alias-mac:ticket-50
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Insulin CLI | ||
| * | ||
| * Copyright (c) 2008-2013 Filipe Guerra, João Morais | ||
| * http://cli.sugarmeetsinsulin.com | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Insulin\Console\Command; | ||
|
|
||
| use Insulin\Sugar\Exception\RuntimeException; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| /** | ||
| * This command provides information about SugarCRM version. | ||
| * | ||
| * You will be able to know which version, flavor and build number your Sugar | ||
| * instance is running. | ||
| * | ||
| * @fixme Insulin commands need to extend from InsulinCommand so it requires an Insulin Application | ||
| * | ||
| * @api | ||
| */ | ||
| class CronCommand extends Command | ||
| { | ||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function configure() | ||
| { | ||
| $this | ||
| ->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( | ||
| <<<EOF | ||
| The <info>%command.name%</info> command runs all active cron jobs: | ||
|
|
||
| <info>%command.full_name%</info> | ||
|
|
||
| You can also run only some specific job or list of jobs by providing the ids: | ||
|
|
||
| <info>%command.full_name% 7542f4f8-7d15-dd03-d6a1-52af6cfee427 804d64aa-f22f-c185-48c1-52af6c580622</info> | ||
|
|
||
| It's also possible to force the cron to run (useful to ignore status and last run time): | ||
| <info>%command.full_name% --force</info> | ||
| 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.'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not doing
BeanFactory::getBean('Schedulers');instead?