Skip to content

Commit 0398879

Browse files
committed
Merge branch 'NeilCrookes'
Removed Progess Controller and Views. Conflicts: models/queued_task.php vendors/shells/queue.php
2 parents d1650c6 + 7b2667d commit 0398879

File tree

5 files changed

+109
-10
lines changed

5 files changed

+109
-10
lines changed

config/sql/queue.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ function after($event = array()) {
3535
'null' => true,
3636
'default' => NULL
3737
),
38+
'group' => array(
39+
'type' => 'string',
40+
'length' => 255,
41+
'null' => true,
42+
'default' => NULL
43+
),
44+
'reference' => array(
45+
'type' => 'string',
46+
'length' => 255,
47+
'null' => true,
48+
'default' => NULL
49+
),
3850
'created' => array(
3951
'type' => 'datetime',
4052
'null' => false
@@ -60,6 +72,11 @@ function after($event = array()) {
6072
'default' => '0',
6173
'length' => 3
6274
),
75+
'failure_message' => array(
76+
'type' => 'text',
77+
'null' => true,
78+
'default' => NULL
79+
),
6380
'workerkey' => array(
6481
'type' => 'string',
6582
'null' => true,

models/queued_task.php

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,27 @@ class QueuedTask extends AppModel {
1414
public $rateHistory = array();
1515

1616
public $exit = false;
17+
18+
public $_findMethods = array(
19+
'progress' => true
20+
);
1721

1822
/**
1923
* Add a new Job to the Queue
2024
*
2125
* @param string $jobName QueueTask name
2226
* @param array $data any array
27+
* @param string $group Used to group similar QueuedTasks
28+
* @param string $reference any array
2329
* @return bool success
2430
*/
25-
public function createJob($jobName, $data, $notBefore = null) {
31+
public function createJob($jobName, $data, $notBefore = null, $group = null, $reference = null) {
2632

2733
$data = array(
2834
'jobtype' => $jobName,
29-
'data' => serialize($data)
35+
'data' => serialize($data),
36+
'group' => $group,
37+
'reference' => $reference
3038
);
3139
if ($notBefore != null) {
3240
$data['notbefore'] = date('Y-m-d H:i:s', strtotime($notBefore));
@@ -39,12 +47,14 @@ public function onError() {
3947
}
4048

4149
/**
42-
* Look for a new job that can be processed with the current abilities.
50+
* Look for a new job that can be processed with the current abilities and
51+
* from the specified group (or any if null).
4352
*
4453
* @param array $capabilities Available QueueWorkerTasks.
54+
* @param string $group Request a job from this group, (from any group if null)
4555
* @return Array Taskdata.
4656
*/
47-
public function requestJob($capabilities) {
57+
public function requestJob($capabilities, $group = null) {
4858
$idlist = array();
4959
$wasFetched = array();
5060

@@ -64,6 +74,11 @@ public function requestJob($capabilities) {
6474
),
6575
'limit' => 3
6676
);
77+
78+
if (!is_null($group)) {
79+
$findConf['conditions']['group'] = $group;
80+
}
81+
6782
// generate the task specific conditions.
6883
foreach ($capabilities as $task) {
6984
$tmp = array(
@@ -141,8 +156,10 @@ public function markJobDone($id) {
141156
* Mark a job as Failed, Incrementing the failed-counter and Requeueing it.
142157
*
143158
* @param integer $id
159+
* @param string $failureMessage Optional message to append to the
160+
* failure_message field
144161
*/
145-
public function markJobFailed($id) {
162+
public function markJobFailed($id, $failureMessage = null) {
146163
$findConf = array(
147164
'conditions' => array(
148165
'id' => $id
@@ -151,6 +168,7 @@ public function markJobFailed($id) {
151168
$data = $this->find('first', $findConf);
152169
if (is_array($data)) {
153170
$data[$this->name]['failed']++;
171+
$data[$this->name]['failure_message'] .= $failureMessage . "\n";
154172
return (is_array($this->save($data)));
155173
}
156174
return false;
@@ -222,5 +240,51 @@ public function cleanOldJobs() {
222240

223241
}
224242

243+
protected function _findProgress($state, $query = array(), $results = array()) {
244+
245+
if ($state == 'before') {
246+
247+
$query['fields'] = array(
248+
'QueuedTask.reference',
249+
'(CASE WHEN QueuedTask.notbefore > NOW() THEN \'NOT_READY\' WHEN QueuedTask.fetched IS NULL THEN \'NOT_STARTED\' WHEN QueuedTask.fetched IS NOT NULL AND QueuedTask.completed IS NULL AND QueuedTask.failed = 0 THEN \'IN_PROGRESS\' WHEN QueuedTask.fetched IS NOT NULL AND QueuedTask.completed IS NULL AND QueuedTask.failed > 0 THEN \'FAILED\' WHEN QueuedTask.fetched IS NOT NULL AND QueuedTask.completed IS NOT NULL THEN \'COMPLETED\' ELSE \'UNKNOWN\' END) AS status',
250+
'QueuedTask.failure_message'
251+
);
252+
253+
if (isset($query['conditions']['exclude'])) {
254+
$exclude = $query['conditions']['exclude'];
255+
unset($query['conditions']['exclude']);
256+
$exclude = trim($exclude, ',');
257+
$exclude = explode(',', $exclude);
258+
$query['conditions'][] = array(
259+
'NOT' => array(
260+
'reference' => $exclude
261+
)
262+
);
263+
}
264+
if (isset($query['conditions']['group'])) {
265+
$query['conditions'][]['QueuedTask.group'] = $query['conditions']['group'];
266+
unset($query['conditions']['group']);
267+
}
268+
269+
return $query;
270+
271+
} else {
272+
273+
foreach ($results as $k => $result) {
274+
$results[$k] = array(
275+
'reference' => $result[$this->alias]['reference'],
276+
'status' => $result[0]['status']
277+
);
278+
if (!empty($result[$this->alias]['failure_message'])) {
279+
$results[$k]['failure_message'] = $result[$this->alias]['failure_message'];
280+
}
281+
}
282+
283+
return $results;
284+
285+
}
286+
287+
}
288+
225289
}
226290
?>

vendors/shells/queue.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public function initialize() {
5050
'defaultworkertimeout' => 120,
5151
'defaultworkerretries' => 4,
5252
'workermaxruntime' => 0,
53-
'cleanuptimeout' => 2000
53+
'cleanuptimeout' => 2000,
54+
'exitwhennothingtodo' => false
5455
), $conf));
5556
}
5657

@@ -116,10 +117,13 @@ public function add() {
116117
public function runworker() {
117118
$exit = false;
118119
$starttime = time();
119-
120+
$group = null;
121+
if (isset($this->params['group']) && !empty($this->params['group'])) {
122+
$group = $this->params['group'];
123+
}
120124
while (!$exit) {
121125
$this->out('Looking for Job....');
122-
$data = $this->QueuedTask->requestJob($this->getTaskConf());
126+
$data = $this->QueuedTask->requestJob($this->getTaskConf(), $group);
123127
if ($this->QueuedTask->exit === true) {
124128
$exit = true;
125129
} else {
@@ -131,9 +135,16 @@ public function runworker() {
131135
$this->QueuedTask->markJobDone($data['id']);
132136
$this->out('Job Finished.');
133137
} else {
134-
$this->QueuedTask->markJobFailed($data['id']);
138+
$failureMessage = null;
139+
if (isset($this->{$taskname}->failureMessage) && !empty($this->{$taskname}->failureMessage)) {
140+
$failureMessage = $this->{$taskname}->failureMessage;
141+
}
142+
$this->QueuedTask->markJobFailed($data['id'], $failureMessage);
135143
$this->out('Job did not finish, requeued.');
136144
}
145+
} elseif (Configure::read('queue.exitwhennothingtodo')) {
146+
$this->out('nothing to do, exiting.');
147+
$exit = true;
137148
} else {
138149
$this->out('nothing to do, sleeping.');
139150
sleep(Configure::read('queue.sleeptime'));

vendors/shells/tasks/queue_email.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class queueEmailTask extends Shell {
3535
* @var Controller
3636
*/
3737
public $Controller;
38+
3839
/**
3940
* EmailComponent
4041
*

vendors/shells/tasks/queue_example.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class queueExampleTask extends Shell {
3030
public $QueuedTask;
3131

3232
/**
33-
* Timeout für run, after which the Task is reassigned to a new worker.
33+
* Timeout f�r run, after which the Task is reassigned to a new worker.
3434
*
3535
* @var integer
3636
*/
@@ -41,6 +41,12 @@ class queueExampleTask extends Shell {
4141
* @var integer
4242
*/
4343
public $retries = 0;
44+
/**
45+
* Stores any failure messages triggered during run()
46+
*
47+
* @var string
48+
*/
49+
public $failureMessage = '';
4450

4551
/**
4652
* Example add functionality.

0 commit comments

Comments
 (0)