1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\System\Cron; |
4
|
|
|
|
5
|
|
|
use Magento\Cron\Model\Schedule; |
6
|
|
|
use Magento\Framework\App\Area; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
class ScheduleCommand extends AbstractCronCommand |
12
|
|
|
{ |
13
|
|
|
protected function configure() |
14
|
|
|
{ |
15
|
|
|
$this |
16
|
|
|
->setName('sys:cron:schedule') |
17
|
|
|
->addArgument('job', InputArgument::OPTIONAL, 'Job code') |
18
|
|
|
->setDescription('Schedule a cronjob for execution right now, by job code'); |
19
|
|
|
$help = <<<HELP |
20
|
|
|
If no `job` argument is passed you can select a job from a list. |
21
|
|
|
HELP; |
22
|
|
|
$this->setHelp($help); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param InputInterface $input |
27
|
|
|
* @param OutputInterface $output |
28
|
|
|
* @throws \Exception |
29
|
|
|
* @return int|void |
30
|
|
|
*/ |
31
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
32
|
|
|
{ |
33
|
|
|
$this->state->setAreaCode(Area::AREA_CRONTAB); |
34
|
|
|
$objectManager = $this->getObjectManager(); |
35
|
|
|
$configLoader = $objectManager->get('Magento\Framework\ObjectManager\ConfigLoaderInterface'); |
36
|
|
|
$objectManager->configure($configLoader->load(Area::AREA_CRONTAB)); |
37
|
|
|
|
38
|
|
|
list($jobCode, $jobConfig) = $this->getJobForExecuteMethod($input, $output); |
39
|
|
|
|
40
|
|
|
$output->write( |
41
|
|
|
'<info>Scheduling </info><comment>' . $jobConfig['instance'] . '::' . $jobConfig['method'] . '</comment> ' |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$createdAtTime = $this->timezone->scopeTimeStamp(); |
45
|
|
|
$scheduledAtTime = $createdAtTime; |
46
|
|
|
|
47
|
|
|
/* @var $schedule \Magento\Cron\Model\Schedule */ |
48
|
|
|
$schedule = $this->cronScheduleCollection->getNewEmptyItem(); |
49
|
|
|
$schedule |
50
|
|
|
->setJobCode($jobCode) |
51
|
|
|
->setStatus(Schedule::STATUS_PENDING) |
52
|
|
|
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $createdAtTime)) |
53
|
|
|
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $scheduledAtTime)) |
54
|
|
|
->save(); |
55
|
|
|
|
56
|
|
|
$output->writeln('<info>done</info>'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|