|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\System\Cron; |
|
4
|
|
|
|
|
5
|
|
|
use Magento\Cron\Model\Schedule; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
|
|
10
|
|
|
class ScheduleCommand extends AbstractCronCommand |
|
11
|
|
|
{ |
|
12
|
|
|
protected function configure() |
|
13
|
|
|
{ |
|
14
|
|
|
$this |
|
15
|
|
|
->setName('sys:cron:schedule') |
|
16
|
|
|
->addArgument('job', InputArgument::OPTIONAL, 'Job code') |
|
17
|
|
|
->setDescription('Schedule a cronjob for execution right now, by job code'); |
|
18
|
|
|
$help = <<<HELP |
|
19
|
|
|
If no `job` argument is passed you can select a job from a list. |
|
20
|
|
|
HELP; |
|
21
|
|
|
$this->setHelp($help); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param InputInterface $input |
|
26
|
|
|
* @param OutputInterface $output |
|
27
|
|
|
* @throws \Exception |
|
28
|
|
|
* @return int|void |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
31
|
|
|
{ |
|
32
|
|
|
$jobCode = $input->getArgument('job'); |
|
33
|
|
|
$jobs = $this->getJobs(); |
|
34
|
|
|
|
|
35
|
|
|
if (!$jobCode) { |
|
36
|
|
|
$this->writeSection($output, 'Cronjob'); |
|
37
|
|
|
$jobCode = $this->askJobCode($input, $output, $jobs); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$jobConfig = $this->getJobConfig($jobCode); |
|
41
|
|
|
|
|
42
|
|
|
if (empty($jobCode) || !isset($jobConfig['instance'])) { |
|
43
|
|
|
throw new \InvalidArgumentException('No job config found!'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$model = $this->getObjectManager()->get($jobConfig['instance']); |
|
47
|
|
|
|
|
48
|
|
View Code Duplication |
if (!$model || !is_callable(array($model, $jobConfig['method']))) { |
|
|
|
|
|
|
49
|
|
|
throw new \RuntimeException( |
|
50
|
|
|
sprintf( |
|
51
|
|
|
'Invalid callback: %s::%s does not exist', |
|
52
|
|
|
$jobConfig['instance'], |
|
53
|
|
|
$jobConfig['method'] |
|
54
|
|
|
) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$output->write( |
|
59
|
|
|
'<info>Scheduling </info><comment>' . $jobConfig['instance'] . '::' . $jobConfig['method'] . '</comment> ' |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
$createdAtTime = $this->timezone->scopeTimeStamp(); |
|
63
|
|
|
$scheduledAtTime = $createdAtTime; |
|
64
|
|
|
|
|
65
|
|
|
$schedule = $this->cronScheduleCollection->getNewEmptyItem(); |
|
66
|
|
|
$schedule |
|
67
|
|
|
->setJobCode($jobCode) |
|
68
|
|
|
->setStatus(Schedule::STATUS_PENDING) |
|
69
|
|
|
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $createdAtTime)) |
|
70
|
|
|
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $scheduledAtTime)) |
|
71
|
|
|
->save(); |
|
72
|
|
|
|
|
73
|
|
|
$output->writeln('<info>done</info>'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.