forked from netz98/n98-magerun2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sys:cron:schedule command, closes #257
Add a command called `sys:cron:schedule` to schedule cronjobs so they get picked up by the next cron run. Motivation: Sometimes we want to run a cronjob, but not keep our ssh session or terminal open while running sys:cron:run. Was inspired by the excellent Aoe_Scheduler module for M1. Additional to the new feature some detailed changes regarding code-style and stability / detail fixes that overall improve the command incl. the base class: * Move the askJobCode method to the cron command base class. * Format the scheduled at time, without seconds, just like Magento does this. * Add a basic test for the new sys:cron:schedule command. * Remove underscores in front of the members in all cron related classes. Also remove the unused 'eventManager' member. * Remove some more unused members and a const from the cron code (and even more unused members). * Fix the timezone of the timestamps of the sys:cron:run command to match Magento's behavior.
- Loading branch information
Showing
9 changed files
with
160 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,75 @@ | ||
<?php | ||
|
||
namespace N98\Magento\Command\System\Cron; | ||
|
||
use Magento\Cron\Model\Schedule; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ScheduleCommand extends AbstractCronCommand | ||
{ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('sys:cron:schedule') | ||
->addArgument('job', InputArgument::OPTIONAL, 'Job code') | ||
->setDescription('Schedule a cronjob for execution right now, by job code'); | ||
$help = <<<HELP | ||
If no `job` argument is passed you can select a job from a list. | ||
HELP; | ||
$this->setHelp($help); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @throws \Exception | ||
* @return int|void | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$jobCode = $input->getArgument('job'); | ||
$jobs = $this->getJobs(); | ||
|
||
if (!$jobCode) { | ||
$this->writeSection($output, 'Cronjob'); | ||
$jobCode = $this->askJobCode($input, $output, $jobs); | ||
} | ||
|
||
$jobConfig = $this->getJobConfig($jobCode); | ||
|
||
if (empty($jobCode) || !isset($jobConfig['instance'])) { | ||
throw new \InvalidArgumentException('No job config found!'); | ||
} | ||
|
||
$model = $this->getObjectManager()->get($jobConfig['instance']); | ||
|
||
if (!$model || !is_callable(array($model, $jobConfig['method']))) { | ||
throw new \RuntimeException( | ||
sprintf( | ||
'Invalid callback: %s::%s does not exist', | ||
$jobConfig['instance'], | ||
$jobConfig['method'] | ||
) | ||
); | ||
} | ||
|
||
$output->write( | ||
'<info>Scheduling </info><comment>' . $jobConfig['instance'] . '::' . $jobConfig['method'] . '</comment> ' | ||
); | ||
|
||
$createdAtTime = $this->timezone->scopeTimeStamp(); | ||
$scheduledAtTime = $createdAtTime; | ||
|
||
$schedule = $this->cronScheduleCollection->getNewEmptyItem(); | ||
$schedule | ||
->setJobCode($jobCode) | ||
->setStatus(Schedule::STATUS_PENDING) | ||
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $createdAtTime)) | ||
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $scheduledAtTime)) | ||
->save(); | ||
|
||
$output->writeln('<info>done</info>'); | ||
} | ||
} |
This file contains 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
This file contains 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
24 changes: 24 additions & 0 deletions
24
tests/N98/Magento/Command/System/Cron/ScheduleCommandTest.php
This file contains 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,24 @@ | ||
<?php | ||
|
||
namespace N98\Magento\Command\System\Cron; | ||
|
||
use N98\Magento\Command\TestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
class ScheduleCommandTest extends TestCase | ||
{ | ||
public function testExecute() | ||
{ | ||
$application = $this->getApplication(); | ||
$application->add(new ListCommand()); | ||
$command = $this->getApplication()->find('sys:cron:schedule'); | ||
|
||
$commandTester = new CommandTester($command); | ||
$commandTester->execute([ | ||
'command' => $command->getName(), | ||
'job' => 'backend_clean_cache', | ||
]); | ||
|
||
$this->assertContains('Scheduling Magento\Backend\Cron\CleanCache::execute done', $commandTester->getDisplay()); | ||
} | ||
} |