1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\System\Cron; |
4
|
|
|
|
5
|
|
|
use AppendIterator; |
6
|
|
|
use Mage; |
7
|
|
|
use Mage_Core_Model_Config_Element; |
8
|
|
|
use Mage_Cron_Exception; |
9
|
|
|
use Mage_Cron_Model_Schedule; |
10
|
|
|
use N98\Magento\Command\AbstractMagentoCommand; |
11
|
|
|
|
12
|
|
|
abstract class AbstractCronCommand extends AbstractMagentoCommand |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
protected function getJobs() |
18
|
|
|
{ |
19
|
|
|
$table = array(); |
20
|
|
|
|
21
|
|
|
// Get job configuration from XML and database. Expression priority is given to the database. |
22
|
|
|
|
23
|
|
|
/** @var $jobs AppendIterator */ |
24
|
|
|
$jobs = array_reduce( |
25
|
|
|
array('crontab/jobs', 'default/crontab/jobs'), function (AppendIterator $carry, $path) { |
26
|
|
|
if ($jobConfig = Mage::getConfig()->getNode($path)) { |
27
|
|
|
$carry->append(new \IteratorIterator($jobConfig->children())); |
28
|
|
|
}; |
29
|
|
|
return $carry; |
30
|
|
|
}, new AppendIterator() |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
foreach ($jobs as $name => $job) { |
34
|
|
|
/* @var $job Mage_Core_Model_Config_Element */ |
35
|
|
|
$table[$name] = array('Job' => $name) + $this->getSchedule($job); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
ksort($table, SORT_STRING); |
39
|
|
|
|
40
|
|
|
return $table; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Mage_Core_Model_Config_Element $job |
45
|
|
|
* @return array of five cron values,keyed by 'm', 'h', 'D', 'M' and 'WD' |
46
|
|
|
*/ |
47
|
|
|
protected function getSchedule(Mage_Core_Model_Config_Element $job) |
48
|
|
|
{ |
49
|
|
|
$keys = array('m', 'h', 'D', 'M', 'WD'); |
50
|
|
|
$expr = null; |
51
|
|
|
|
52
|
|
|
if (isset($job->schedule->config_path)) { |
53
|
|
|
$expr = Mage::getStoreConfig((string) $job->schedule->config_path); |
54
|
|
|
} elseif (isset($job->schedule->cron_expr)) { |
55
|
|
|
$expr = $job->schedule->cron_expr; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($cronExpressions = $this->parseCronExpression($expr)) { |
59
|
|
|
return array_combine($keys, $cronExpressions); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return array_combine($keys, array_fill(0, 5, ' ')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* parse a cron expression into an array, false-ly if unable to handle |
67
|
|
|
* |
68
|
|
|
* uses magento 1 internal parser of cron expressions |
69
|
|
|
* |
70
|
|
|
* @return array with five values (zero-indexed) or FALSE in case it does not exists. |
71
|
|
|
*/ |
72
|
|
|
private function parseCronExpression($expr) { |
73
|
|
|
|
74
|
|
|
if ($expr === 'always') { |
75
|
|
|
return array_fill(0, 5, '*'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** @var $schedule Mage_Cron_Model_Schedule */ |
79
|
|
|
$schedule = $this->_getModel('cron/schedule', 'Mage_Cron_Model_Schedule'); |
80
|
|
|
|
81
|
|
|
try { |
82
|
|
|
$schedule->setCronExpr($expr); |
83
|
|
|
} catch (Mage_Cron_Exception $e) { |
|
|
|
|
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$array = $schedule->getData('cron_expr_arr'); |
88
|
|
|
|
89
|
|
|
$count = 0; |
90
|
|
|
foreach ($array as $expression) { |
91
|
|
|
if (++$count > 5) { |
92
|
|
|
// year is optional and never parsed |
93
|
|
|
break; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// parse each entry |
97
|
|
|
try { |
98
|
|
|
$schedule->matchCronExpression($expression, 1); |
99
|
|
|
} catch (Mage_Cron_Exception $e) { |
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $array; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|