-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_task.php
307 lines (285 loc) · 11.2 KB
/
scheduler_task.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
require_once 'abstract.php';
class Aoe_Scheduler_Shell_Scheduler_Task extends Mage_Shell_Abstract {
const PHP_PATH = '/usr/bin/php';
/**
* Run script
*
* @return void
*/
public function run() {
$action = $this->getArg('action');
$configf = $this->getArg('config');
if (empty($action) || empty($configf)) {
echo $this->usageHelp();
} else {
$actionMethodName = $action.'Action';
if (method_exists($this, $actionMethodName)) {
if( !file_exists($configf) ) { echo "Config file $configf not found!"; exit(1); }
$config = json_decode(file_get_contents($configf));
$this->$actionMethodName($config);
} else {
echo "Action $action not found!\n";
echo $this->usageHelp();
exit(1);
}
}
}
/**
* Retrieve Usage Help Message
*
* @return string
*/
public function usageHelp() {
$help = 'Available actions: ' . "\n";
$methods = get_class_methods($this);
foreach ($methods as $method) {
if (substr($method, -6) == 'Action') {
$help .= ' -action ' . substr($method, 0, -6);
$helpMethod = $method.'Help';
if (method_exists($this, $helpMethod)) {
$help .= $this->$helpMethod();
}
$help .= "\n";
}
}
return $help;
}
private function getOnlyOne($job) {
/*
$settings = array(
'aoecachecleaner' => array(
'oo_timeout' => 300,
)
);
return $settings[$job];
*/
return array('oo_timeout' => 18000);
}
private function getAnnotations($job) {
$settings = array(
'aoecachecleaner' => array(
'rate_limit' => '10/s',
)
);
return $settings[$job];
}
private function getRouteSettings($job) {
$settings = array(
'aoecachecleaner' => 'web1queue',
'catalog_product_index_price_reindex_all' => 'web1queue',
'enterprise_catalog_index_refresh_price' => 'web1queue',
'enterprise_index_clean_changelog' => 'web1queue',
'enterprise_refresh_index' => 'web1queue',
'enterprise_search_index_reindex_all' => 'web1queue',
'enterprise_targetrule_index_reindex' => 'web1queue',
'M2ePro_cron' => 'web1queue',
'Edi_File_Processors' => 'web1queue',
'reindexall' => 'web1queue'
);
return $settings[$job];
}
/**
* Create a Celery Config File
*
* @return void
*/
public function buildConfigFileAction($config) {
$filename = $config->file;
if (empty($filename)) {
echo "\nBuildConfigFileAction: $filename not found!\n\n";
echo $this->usageHelp();
exit(1);
}
if (empty($config->host)) {
echo "\nNo broker host provided!\n\n";
echo $this->usageHelp();
exit(2);
}
$f = @fopen($filename, 'w');
fwrite($f, '#!/usr/bin/env python2.7'."\n\n");
fwrite($f, 'from __future__ import absolute_import'."\n");
fwrite($f, 'from celery.schedules import crontab'."\n");
fwrite($f, 'BROKER_URL = \'redis://'.$config->host.':'.$config->port.'/'.$config->db.'\''."\n");
fwrite($f, 'BROKER_TRANSPORT_OPTIONS = {\'visibility_timeout\': 3600} # Seconds to wait before message is redelivered to another broker'."\n");
fwrite($f, 'CELERY_TASK_SERIALIZER = \''.$config->task_serializer.'\''."\n");
fwrite($f, 'CELERY_TIMEZONE = \''.$config->timezone.'\''."\n");
fwrite($f, 'CELERY_ENABLE_UTC = '.$config->utc.''."\n\n");
fwrite($f, '# List of modules to import when celery starts.'."\n");
fwrite($f, 'CELERY_IMPORTS = ("mage_scheduler.tasks", )'."\n\n");
fwrite($f, '# Using redis to store task state and results.'."\n");
fwrite($f, 'CELERY_RESULT_SERIALIZER = \''.$config->result_serializer.'\''."\n");
fwrite($f, 'CELERYD_CONCURRENCY = \''.$config->concurrency.'\''."\n");
fwrite($f, '# CELERY_RESULT_BACKEND = \''.$config->behost.':'.$config->beport.'/'.$config->bedb.'\''."\n\n");
$collection = Mage::getModel('aoe_scheduler/collection_crons');
// Add any routes that exist for this task
fwrite($f, 'CELERY_ROUTES = {'."\n");
foreach ($collection as $configuration) {
if( $configuration->getStatus() == 'enabled' ) {
$route = $this->getRouteSettings($configuration->getId());
if($route) {
fwrite($f, ' \'mage_scheduler.tasks.'.$configuration->getId().'\': {\'queue\': \''.$route.'\'},'."\n");
}
}
}
// Addition of reindexall
fwrite($f, ' \'mage_scheduler.tasks.reindexall\': {\'queue\': \''.$this->getRouteSettings('reindexall').'\'},'."\n");
fwrite($f, '}'."\n");
// Add any annotations that exists for this task
fwrite($f, 'CELERY_ANNOTATIONS = {'."\n");
foreach ($collection as $configuration) {
if( $configuration->getStatus() == 'enabled' ) {
$annotations = $this->getAnnotations($configuration->getId());
if($annotations) {
fwrite($f, ' \'mage_scheduler.tasks.'.$configuration->getId().'\': {');
foreach ($annotations as $annotation => $v) {
fwrite($f, '\''.$annotation.'\': \''.$v.'\',');
}
fwrite($f, ' }'."\n");
}
}
}
// Add any annotations for reindexall
$annotations = $this->getAnnotations('reindexall');
if($annotations) {
fwrite($f, ' \'mage_scheduler.tasks.reindexall\': {');
foreach ($annotations as $annotation => $v) {
fwrite($f, '\''.$annotation.'\': \''.$v.'\',');
}
fwrite($f, ' }'."\n");
}
fwrite($f, '}'."\n");
// Add a Scheduler for this Task
fwrite($f, 'CELERYBEAT_SCHEDULE = {'."\n");
foreach ($collection as $configuration) {
if( $configuration->getStatus() == 'enabled' ) {
$cron = explode(" ", $configuration->getCronExpr(), -1);
if( $cron ) {
fwrite($f, ' \''.$configuration->getId().'\': {'."\n");
fwrite($f, ' \'task\': \'mage_scheduler.tasks.'.$configuration->getId().'\','."\n");
fwrite($f, ' \'schedule\': crontab(minute=\''.$cron[0].'\', hour=\''.$cron[1].'\', day_of_month=\''.$cron[2].'\', month_of_year=\''.$cron[3].'\', day_of_week=\'*\'),'."\n");
//} else {
// fwrite($f, ' \'schedule\': crontab(minute=\'*/1\', hour=\'*\', day_of_month=\'*\', month_of_year=\'*\', day_of_week=\'*\'),'."\n");
fwrite($f, ' \'args\': (\''.Mage::getBaseDir('base').'/shell\',),'."\n");
fwrite($f, ' },'."\n");
}
}
}
// Add Schedule for reindexall
$cron = array("0", "3", "*", "*", "*");
if( $cron ) {
fwrite($f, ' \'reindexall\': {'."\n");
fwrite($f, ' \'task\': \'mage_scheduler.tasks.reindexall\','."\n");
fwrite($f, ' \'schedule\': crontab(minute=\''.$cron[0].'\', hour=\''.$cron[1].'\', day_of_month=\''.$cron[2].'\', month_of_year=\''.$cron[3].'\', day_of_week=\''.$cron[4].'\'),'."\n");
fwrite($f, ' \'args\': (\''.Mage::getBaseDir('base').'/shell\',),'."\n");
fwrite($f, ' },'."\n");
}
///////
fwrite($f, '}'."\n");
fclose($f);
}
/**
* Display extra help for buildConfigFile
*
* @return string
*/
public function buildConfigFileActionHelp() {
return " -config <file>\tCreate Celery config file.";
}
/**
* print out individual Task info.
*/
private function getTaskStr($id, $php_cmd, $args) {
$oo = $this->getOnlyOne($id);
$taskstr = "\n\n\n".'@celery.task'."\n";
$taskstr .= 'class '.$id.'(Task):'."\n";
if($oo) {
$taskstr .= ' @only_one(key="'.$id.'", timeout='.$oo['oo_timeout'].')'."\n";
}
$taskstr .= ' def run(self, shell_dir):'."\n";
$taskstr .= ' cmd = \'%s/'.$php_cmd.' '.implode(' ', $args).'\' % (shell_dir)'."\n";
$taskstr .= ' logger.info(cmd)'."\n";
$taskstr .= ' os.chdir(shell_dir)'."\n";
$taskstr .= ' retcode = os.system(\''.self::PHP_PATH.' %s\' % (cmd))'."\n";
$taskstr .= ' if retcode > 0:'."\n";
$taskstr .= ' raise Exception(\'Error: %d\' % (retcode))'."\n";
$taskstr .= ' return \'SUCCESS\'';
return $taskstr;
}
/**
* Create a Celery Task File
*
* In the next version phpexe should be an abstract celery task class, and
* all custom tasks should extend it and call phpexe's run function with a
* code argument, but this is quicker for now.
*
* @return void
*/
public function buildTaskFileAction($config) {
$filename = $config->tfile;
if (empty($filename)) {
echo "\nBuildTaskFileAction: $filename not found!\n\n";
echo $this->usageHelp();
exit(1);
}
$f = @fopen($filename, 'w');
fwrite($f, '#!/usr/bin/env python2.7'."\n\n");
fwrite($f, 'from __future__ import absolute_import'."\n");
fwrite($f, 'from celery.utils.log import get_task_logger'."\n");
fwrite($f, 'from celery.task import Task'."\n");
fwrite($f, 'from mage_scheduler.celery import celery'."\n");
fwrite($f, 'from mage_scheduler.only_one import only_one'."\n");
fwrite($f, 'import os'."\n\n");
fwrite($f, 'logger = get_task_logger(__name__)');
$collection = Mage::getModel('aoe_scheduler/collection_crons');
foreach ($collection as $configuration) {
if( $configuration->getStatus() == 'enabled' ) {
fwrite($f,
$this->getTaskStr($configuration->getId(),
'scheduler.php',
array('-action runNow', '-code '.$configuration->getId())));
}
}
fwrite($f, $this->getTaskStr('reindexall', 'indexer.php', array('reindexall')));
fclose($f);
}
/**
* Display extra help for buildTaskFile
*
* @return string
*/
public function buildTaskFileActionHelp() {
return " -config <file>\tCreate a Celery tasks file.";
}
/**
* BuildTaskFile and BuildConfigFile
*
* @return string
*/
public function buildAction($config) {
$farg = $config->file;
$targ = $config->tfile;
if (empty($farg) || empty($targ)) {
echo "\nNo File name(s) found!\n\n";
echo $this->usageHelp();
exit(1);
}
if (empty($config->host)) {
echo "\nNo broker host provided!\n\n";
echo $this->usageHelp();
exit(2);
}
$this->buildTaskFileAction($config);
$this->buildConfigFileAction($config);
}
/**
* Display extra help for build
*
* @return string
*/
public function buildActionHelp() {
return " -config <file>\t\tCreate both the Celery task and config files.";
}
}
$shell = new Aoe_Scheduler_Shell_Scheduler_Task();
$shell->run();