-
Notifications
You must be signed in to change notification settings - Fork 50
/
Job.php
381 lines (350 loc) · 12 KB
/
Job.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* Job model. Jobs are run with a variety of options as defined in config.xml as shown below.
*
* Remember that these options can be overridden in local.xml, for example to temporarily disable a group of tasks
* or to adjust the priorities of a task.
*
* <config>
* <mongo_queue>
* <tasks>
* <your_task_name>
* <type>singleton</type> <!-- What type of class should be used? helper|model|resource|singleton (default: singleton) -->
* <class>foo/bar</class> <!-- Path to load class by, according to <type> -->
* <method>runQueue</method> <!-- Method to run. Signature: (Varien_Object $data, Cm_Mongo_Model_Job $job) -->
* <load_index>_id</load_index> <!-- If type is 'model' the model will be loaded with the data at this index before the method is called. -->
* <retries>3</retries> <!-- Set the maximum number of retries for this task -->
* <retry_schedule> <!-- If no retry schedule is given retries are scheduled immediately -->
* +3 min, +1 hour, +12 hours <!-- The schedule is a list of intervals which are strtotime compatible offsets -->
* </retry_schedule>
* <priorities> <!-- Set the default priority for this task. 50 is the default unless specified -->
* 50,60,100 <!-- If multiple values are given the next priority will be used on each retry -->
* </priorities>
* <after_success>keep</after_success> <!-- If 'keep', the job will not be deleted after it is completed successfully -->
* <after_failure>delete</after_failure> <!-- If 'delete', the job will be deleted after failure (retries exhausted) -->
* <logging/> <!-- Enable or disable logging of errors -->
* <logfile>foo_queue.log</logfile> <!-- Set the logging destination. Defaults to mongo_queue_errors.txt -->
* <disabled/> <!-- Disable a specific task -->
* <disabled>false</disabled> <!-- Override a previous "disabled" flag -->
* <groups>foo,bar</groups> <!-- Assign to one or more groups so a task can be disabled with other tasks as a group -->
* </your_task_name>
* <your_task_namespace>
* <one> <!-- Task nested in a namespace: 'your_task_namespace/one' -->
* <type>helper</type>
* ...
* </one>
* </your_task_namespace>
* </tasks>
* <groups>
* <foo>
* <disabled/> <!-- any tasks in "foo" group are disabled! -->
* </foo>
* </groups>
* </mongo_queue>
* </config>
*
* Sample job method:
*
* public function downloadFooJob(Varien_Object $data, Cm_Mongo_Model_Job $job)
* {
* if( ! $data->getFoo()) {
* $job->denyRetry();
* throw new Exception('No foo!');
* }
* $foo = file_get_contents($foo);
* if( ! $foo) {
* throw new Exception('Could not download foo.');
* }
* // Do something with $foo
* }
*
* Document Fields:
* _id
* task
* job_data
* status
* status_message
* pid
* retries
* priority
* execute_at
* error_log
* other_data
*
* @method Cm_Mongo_Model_Mongo_Job getResource()
*/
class Cm_Mongo_Model_Job extends Cm_Mongo_Model_Abstract
{
const STATUS_READY = 'ready';
const STATUS_RUNNING = 'running';
const STATUS_INVALID = 'invalid';
const STATUS_DISABLED = 'disabled';
const STATUS_SUCCESS = 'success';
const STATUS_FAILED = 'failed';
const LOGFILE = 'mongo_queue_errors.txt';
const DEFAULT_PRIORITY = 50;
protected function _construct()
{
$this->_init('mongo/job');
}
/**
* @return boolean
*/
public function isTaskDisabled()
{
// Allow override
if($this->getForceEnabled()) {
return false;
}
// Check specific task config
if($this->getTaskConfig()->is('disabled')) {
return true;
}
// Check group config if a group is specified
if($groups = (string) $this->getTaskConfig('groups')) {
foreach(explode(',',$groups) as $group) {
if(Mage::app()->getConfig()->getNode('mongo_queue/groups/'.$group)->is('disabled')) {
return true;
}
}
}
return false;
}
/**
* @param string $node
* @return Mage_Core_Model_Config_Element
*/
public function getTaskConfig($node = '')
{
$node = 'mongo_queue/tasks/'.$this->getTask().($node ? "/$node" : '');
return Mage::app()->getConfig()->getNode($node);
}
/**
* @param string $status
* @param string|NULL $statusMessage
* @return Cm_Mongo_Model_Job
*/
public function updateStatus($status, $statusMessage = NULL)
{
$this->setStatus($status)
->setStatusMessage($statusMessage)
->setPid(NULL);
return $this;
}
/**
* Ensure job will not be retried on failure even if "retries" config has not been reached.
*
* @return Cm_Mongo_Model_Job
*/
public function denyRetry()
{
return $this->setCanRetry(FALSE);
}
/**
* Check if the job can be scheduled for retry on failure. Can be overridden witth denyRetry().
*
* @return boolean
*/
public function canRetry()
{
if($this->hasData('can_retry')) {
return $this->getCanRetry();
}
$maxRetries = (int) $this->getTaskConfig('retries');
return $maxRetries && $maxRetries > $this->getRetries();
}
/**
* Schedule a retry based on the parameters or the task configuration.
* This is automatically called if an exception is thrown and canRetry() is true.
*
* @param null|string|int|MongoDate $time A strtotime compatible string, a unix timestamp or a MongoDate
* @return Cm_Mongo_Model_Job
*/
public function scheduleRetry($time = NULL)
{
$retries = (int) $this->getRetries();
// Update the execute_at time
if($time === NULL) {
$time = new MongoDate;
$schedule = (string) $this->getTaskConfig('retry_schedule');
if($schedule) {
$schedule = preg_split('/\s*,\s*/', $schedule, null, PREG_SPLIT_NO_EMPTY);
$modifier = isset($schedule[$retries]) ? $schedule[$retries] : end($schedule);
$time = new MongoDate(strtotime($modifier, $time->sec));
}
}
else if(is_string($time)) {
$time = new MongoDate(strtotime($time));
}
else if(is_int($time)) {
$time = new MongoDate($time);
}
// Update the priority unless it was already set explicitly
if( ! $this->dataHasChangedFor('priority')) {
$priorities = (string) $this->getTaskConfig('priorities');
$priorities = preg_split('/\s*,\s*/', $priorities, null, PREG_SPLIT_NO_EMPTY);
if(count($priorities) > 1) {
$this->setPriority(isset($priorities[$retries+1]) ? $priorities[$retries+1] : end($priorities));
}
}
// Reset to ready status
$this->updateStatus(self::STATUS_READY)
->op('$inc', 'retries', 1)
->setExecuteAt($time);
return $this;
}
/**
* @param string|Exception $e
* @return Cm_Mongo_Model_Job
*/
public function logError($e)
{
if($this->getTaskConfig()->is('logging'))
{
$logfile = (string) $this->getTaskConfig('logfile');
if( ! $logfile) {
$logfile = self::LOGFILE;
}
$message = "PID:{$this->getPid()}, Retries:{$this->getRetries()}, ".
"Task:{$this->getTask()}, Data:".json_encode($this->getJobData())."\n".
"Error: $e";
Mage::log($message, Zend_Log::DEBUG, $logfile);
}
$this->op('$push', 'error_log', array('time' => new MongoDate(), 'message' => "$e"));
return $this;
}
/**
* Executes the task and updates the status.
*
* @throws Exception on an unexpected configuration error.
*/
public function run()
{
if($this->getStatus() != self::STATUS_RUNNING) {
throw new Exception('Cannot run job when status is not \'running\'.');
}
// Mark invalid if task not defined
if( ! $this->getTaskConfig('class')) {
$this->updateStatus(self::STATUS_INVALID, 'Task not properly defined.')
->save();
return;
}
// If task is globally disabled, put job on hold.
if($this->isTaskDisabled()) {
$this->updateStatus(self::STATUS_DISABLED)
->save();
return;
}
// Load task
switch((string) $this->getTaskConfig('type')) {
case 'helper':
$object = Mage::helper((string)$this->getTaskConfig('class'));
break;
case 'model':
$object = Mage::getModel((string)$this->getTaskConfig('class'));
break;
case 'resource':
$object = Mage::getResourceSingleton((string)$this->getTaskConfig('class'));
break;
case 'singleton':
default:
$object = Mage::getSingleton((string)$this->getTaskConfig('class'));
break;
}
if( ! $object) {
$this->updateStatus(self::STATUS_INVALID, 'Could not get task model.')
->save();
return;
}
// Confirm existence of method
$method = (string) $this->getTaskConfig('method');
if( ! method_exists($object, $method)) {
$this->updateStatus(self::STATUS_INVALID, 'Cannot call task method.')
->save();
return;
}
$data = new Varien_Object($this->getJobData());
// Run task
try {
// Load object if a load_index is specified
if($this->getTaskConfig('type') == 'model' && ($loadIndex = $this->getTaskConfig('load_index'))) {
if( ! $data->hasData($loadIndex)) {
throw new Exception('No id in job data to load by.');
}
$object->load($data->getData($loadIndex), $loadIndex);
$data->unsetData($loadIndex);
if( ! $object->getId()) {
throw new Exception('Object could not be loaded.');
}
}
$object->$method($data, $this);
// If status not changed by task, assume success
if($this->getStatus() == self::STATUS_RUNNING || $this->getStatus() == self::STATUS_SUCCESS) {
// Update or delete?
if($this->getTaskConfig()->is('after_success','keep')) {
$this->updateStatus(self::STATUS_SUCCESS);
}
// Delete by default
else {
$this->isDeleted(true);
}
}
}
// If exception caught, retry if possible
catch(Exception $e) {
if($this->getStatus() == self::STATUS_RUNNING) {
$this->logError($e);
// Retry
if($this->canRetry()) {
$this->scheduleRetry();
}
// Delete if specified
else if($this->getTaskConfig()->is('after_failure','delete')) {
$this->isDeleted(true);
}
// Keep by default
else {
$this->updateStatus(self::STATUS_FAILED, $e->getMessage());
}
}
}
$this->save();
}
/**
* Method for running via Magento cron
*/
public function runCron()
{
if( ! Mage::getStoreConfigFlag('system/mongo_queue/cron_enabled')) {
return;
}
$limit = (int) Mage::getStoreConfig('system/mongo_queue/limit');
$time = (int) Mage::getStoreConfig('system/mongo_queue/time');
Mage::helper('mongo/queue')->runQueue($limit, $time);
}
protected function _beforeSave()
{
// Make sure a priority is set
if($this->getData('priority') === null) {
$priorities = $this->getTaskConfig('priorities');
if($priorities === false) {
$priority = Cm_Mongo_Model_Job::DEFAULT_PRIORITY;
} else {
$priorities = preg_split('/\s*,\s*/', $priorities, null, PREG_SPLIT_NO_EMPTY);
$priority = $priorities[0];
}
$this->setData('priority', (int) $priority);
}
// Ensure that status updates are consistent
if($this->isObjectNew() === false && $this->dataHasChangedFor('status')) {
$this->setAdditionalSaveCriteria(array('status' => $this->getOrigData('status')));
}
}
protected function _afterSave()
{
// Throw errors if a status update fails
if($this->getLastUpdateStatus() === false) {
throw new Exception("Failed to update job status to {$this->getStatus()} ({$this->getId()})");
}
}
}