|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\System\Cron; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Helper\DialogHelper; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Magento\Cron\Model\Schedule; |
|
10
|
|
|
|
|
11
|
|
|
class RunCommand extends AbstractCronCommand |
|
12
|
|
|
{ |
|
13
|
|
|
const REGEX_RUN_MODEL = '#^([a-z0-9_]+/[a-z0-9_]+)::([a-z0-9_]+)$#i'; |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $infos; |
|
18
|
|
|
|
|
19
|
|
|
protected function configure() |
|
20
|
|
|
{ |
|
21
|
|
|
$this |
|
22
|
|
|
->setName('sys:cron:run') |
|
23
|
|
|
->addArgument('job', InputArgument::OPTIONAL, 'Job code') |
|
24
|
|
|
->setDescription('Runs a cronjob by job code'); |
|
25
|
|
|
$help = <<<HELP |
|
26
|
|
|
If no `job` argument is passed you can select a job from a list. |
|
27
|
|
|
See it in action: http://www.youtube.com/watch?v=QkzkLgrfNaM |
|
28
|
|
|
HELP; |
|
29
|
|
|
$this->setHelp($help); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param InputInterface $input |
|
34
|
|
|
* @param OutputInterface $output |
|
35
|
|
|
* @throws \Exception |
|
36
|
|
|
* @return int|void |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
39
|
|
|
{ |
|
40
|
|
|
$jobCode = $input->getArgument('job'); |
|
41
|
|
|
$jobs = $this->getJobs(); |
|
42
|
|
|
|
|
43
|
|
|
if (!$jobCode) { |
|
44
|
|
|
$this->writeSection($output, 'Cronjob'); |
|
45
|
|
|
$jobCode = $this->askJobCode($input, $output, $jobs); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$jobConfig = $this->getJobConfig($jobCode); |
|
49
|
|
|
|
|
50
|
|
|
if (empty($jobCode) || !isset($jobConfig['instance'])) { |
|
51
|
|
|
throw new \InvalidArgumentException('No job config found!'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$model = $this->getObjectManager()->get($jobConfig['instance']); |
|
55
|
|
|
|
|
56
|
|
|
if (!$model || !is_callable(array($model, $jobConfig['method']))) { |
|
57
|
|
|
throw new \RuntimeException( |
|
58
|
|
|
sprintf( |
|
59
|
|
|
'Invalid callback: %s::%s does not exist', |
|
60
|
|
|
$jobConfig['instance'], |
|
61
|
|
|
$jobConfig['method'] |
|
62
|
|
|
) |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$callback = array($model, $jobConfig['method']); |
|
67
|
|
|
|
|
68
|
|
|
$output->write( |
|
69
|
|
|
'<info>Run </info><comment>' . $jobConfig['instance'] . '::' . $jobConfig['method'] . '</comment> ' |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
$schedule = $this->_cronScheduleCollection->getNewEmptyItem(); |
|
74
|
|
|
$schedule |
|
75
|
|
|
->setJobCode($jobCode) |
|
76
|
|
|
->setStatus(Schedule::STATUS_RUNNING) |
|
77
|
|
|
->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', time())) |
|
78
|
|
|
->save(); |
|
79
|
|
|
|
|
80
|
|
|
$this->_state->emulateAreaCode('crontab', $callback, array($schedule)); |
|
81
|
|
|
|
|
82
|
|
|
$schedule |
|
83
|
|
|
->setStatus(Schedule::STATUS_SUCCESS) |
|
84
|
|
|
->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', time())) |
|
85
|
|
|
->save(); |
|
86
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
87
|
|
|
$schedule |
|
88
|
|
|
->setStatus(Schedule::STATUS_ERROR) |
|
89
|
|
|
->setMessages($e->getMessage()) |
|
90
|
|
|
->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', time())) |
|
91
|
|
|
->save(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$output->writeln('<info>done</info>'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param InputInterface $input |
|
99
|
|
|
* @param OutputInterface $output |
|
100
|
|
|
* @param array $jobs |
|
101
|
|
|
* @return string |
|
102
|
|
|
* @throws \InvalidArgumentException |
|
103
|
|
|
* @throws \Exception |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function askJobCode(InputInterface $input, OutputInterface $output, $jobs) |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
View Code Duplication |
foreach ($jobs as $key => $job) { |
|
|
|
|
|
|
108
|
|
|
$question[] = '<comment>[' . ($key + 1) . ']</comment> ' . $job['Job'] . PHP_EOL; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
$question[] = '<question>Please select job: </question>' . PHP_EOL; |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** @var $dialog DialogHelper */ |
|
113
|
|
|
$dialog = $this->getHelper('dialog'); |
|
114
|
|
|
$jobCode = $dialog->askAndValidate( |
|
115
|
|
|
$output, |
|
116
|
|
|
$question, |
|
117
|
|
|
function ($typeInput) use ($jobs) { |
|
118
|
|
|
if (!isset($jobs[$typeInput - 1])) { |
|
119
|
|
|
throw new \InvalidArgumentException('Invalid job'); |
|
120
|
|
|
} |
|
121
|
|
|
return $jobs[$typeInput - 1]['Job']; |
|
122
|
|
|
} |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
return $jobCode; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
Scrutinizer analyzes your
composer.json/composer.lockfile if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.