|
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 RunCommand extends AbstractCronCommand |
|
11
|
|
|
{ |
|
12
|
|
|
protected function configure() |
|
13
|
|
|
{ |
|
14
|
|
|
$this |
|
15
|
|
|
->setName('sys:cron:run') |
|
16
|
|
|
->addArgument('job', InputArgument::OPTIONAL, 'Job code') |
|
17
|
|
|
->setDescription('Runs a cronjob by job code'); |
|
18
|
|
|
$help = <<<HELP |
|
19
|
|
|
If no `job` argument is passed you can select a job from a list. |
|
20
|
|
|
See it in action: http://www.youtube.com/watch?v=QkzkLgrfNaM |
|
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
|
|
|
$jobCode = $input->getArgument('job'); |
|
34
|
|
|
$jobs = $this->getJobs(); |
|
35
|
|
|
|
|
36
|
|
|
if (!$jobCode) { |
|
37
|
|
|
$this->writeSection($output, 'Cronjob'); |
|
38
|
|
|
$jobCode = $this->askJobCode($input, $output, $jobs); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$jobConfig = $this->getJobConfig($jobCode); |
|
42
|
|
|
|
|
43
|
|
|
if (empty($jobCode) || !isset($jobConfig['instance'])) { |
|
44
|
|
|
throw new \InvalidArgumentException('No job config found!'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$model = $this->getObjectManager()->get($jobConfig['instance']); |
|
48
|
|
|
|
|
49
|
|
View Code Duplication |
if (!$model || !is_callable(array($model, $jobConfig['method']))) { |
|
|
|
|
|
|
50
|
|
|
throw new \RuntimeException( |
|
51
|
|
|
sprintf( |
|
52
|
|
|
'Invalid callback: %s::%s does not exist', |
|
53
|
|
|
$jobConfig['instance'], |
|
54
|
|
|
$jobConfig['method'] |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$callback = array($model, $jobConfig['method']); |
|
60
|
|
|
|
|
61
|
|
|
$output->write( |
|
62
|
|
|
'<info>Run </info><comment>' . $jobConfig['instance'] . '::' . $jobConfig['method'] . '</comment> ' |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
try { |
|
66
|
|
|
$schedule = $this->cronScheduleCollection->getNewEmptyItem(); |
|
67
|
|
|
$schedule |
|
68
|
|
|
->setJobCode($jobCode) |
|
69
|
|
|
->setStatus(Schedule::STATUS_RUNNING) |
|
70
|
|
|
->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp())) |
|
71
|
|
|
->save(); |
|
72
|
|
|
|
|
73
|
|
|
$this->state->emulateAreaCode('crontab', $callback, array($schedule)); |
|
74
|
|
|
|
|
75
|
|
|
$schedule |
|
76
|
|
|
->setStatus(Schedule::STATUS_SUCCESS) |
|
77
|
|
|
->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp())) |
|
78
|
|
|
->save(); |
|
79
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
80
|
|
|
$schedule |
|
81
|
|
|
->setStatus(Schedule::STATUS_ERROR) |
|
82
|
|
|
->setMessages($e->getMessage()) |
|
83
|
|
|
->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp())) |
|
84
|
|
|
->save(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$output->writeln('<info>done</info>'); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
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.