FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Allow jobs to perform a pre-run check and be requeued by egerlach · Pull Request #3 · TribeHR/cakephp_queue · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .php  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 17 additions & 0 deletions models/queued_task.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@ public function markJobFailed($id, $failureMessage = null) {
)));
}

/**
* Requeue a job without incrementing its failure count. Used when a job could not be attempted.
*
* @param integer $id
* @param integer $timeout Number of seconds to wait before trying this job again.
*/
public function requeueJob($id, $timeout) {
$db =& $this->getDataSource();
return ($this->updateAll(array(
'fetched' => null,
'workerkey' => null,
'notbefore' => 'DATE_ADD(NOW(), INTERVAL ' . intval($timeout) . ' SECOND)'
), array(
'id' => $id
)));
}

/**
* Returns the number of items in the Queue.
* Either returns the number of ALL pending tasks, or the number of pending tasks of the passed Type
Expand Down
34 changes: 23 additions & 11 deletions vendors/shells/queue.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,23 @@ public function runworker() {
if ($data !== false) {
$this->out('Running Job of type "' . $data['jobtype'] . '"');
$taskname = 'queue_' . strtolower($data['jobtype']);
$return = $this->{$taskname}->run(unserialize($data['data']));
if ($return == true) {
$this->QueuedTask->markJobDone($data['id']);
$this->out('Job Finished.');
$jobData = unserialize($data['data']);
if (!$this->{$taskname}->canRun($jobData)) {
$this->QueuedTask->requeueJob($data['id'], $this->getTaskConf($taskname, 'timeout'));
$this->out('Job could not be run, requeued.');
} else {
$failureMessage = null;
if (isset($this->{$taskname}->failureMessage) && !empty($this->{$taskname}->failureMessage)) {
$failureMessage = $this->{$taskname}->failureMessage;
$return = $this->{$taskname}->run($jobData);
if ($return == true) {
$this->QueuedTask->markJobDone($data['id']);
$this->out('Job Finished.');
} else {
$failureMessage = null;
if (isset($this->{$taskname}->failureMessage) && !empty($this->{$taskname}->failureMessage)) {
$failureMessage = $this->{$taskname}->failureMessage;
}
$this->QueuedTask->markJobFailed($data['id'], $failureMessage);
$this->out('Job did not finish, requeued.');
}
$this->QueuedTask->markJobFailed($data['id'], $failureMessage);
$this->out('Job did not finish, requeued.');
}
} elseif (Configure::read('queue.exitwhennothingtodo')) {
$this->out('nothing to do, exiting.');
Expand Down Expand Up @@ -227,7 +233,7 @@ public function stats() {
* Returns a List of available QueueTasks and their individual configurations.
* @return array
*/
private function getTaskConf() {
private function getTaskConf($taskname = null, $field = null) {
if (!is_array($this->taskConf)) {
$this->taskConf = array();
foreach ($this->tasks as $task) {
Expand All @@ -247,7 +253,13 @@ private function getTaskConf() {
}
}
}
return $this->taskConf;
if (is_null($taskname)) {
return $this->taskConf;
}
if (is_null($field)) {
return $this->taskConf[$taskname];
}
return $this->taskConf[$taskname][$field];
}
/**
* Output a list of available tasks.
Expand Down

Back | FazBrowse Home | New Git URL