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

New Feature: tasks can be set, to NOT run before a certain time. · keycoder/cakephp_queue@55c3feb · GitHub

Commit 55c3feb

Browse files
committed
New Feature: tasks can be set, to NOT run before a certain time.
1 parent 7e092ae commit 55c3feb

4 files changed

Lines changed: 43 additions & 7 deletions

File tree

‎config/sql/queue.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ function after($event = array()) {
3939
'type' => 'datetime',
4040
'null' => false
4141
),
42+
'notbefore' => array(
43+
'type' => 'datetime',
44+
'null' => true,
45+
'default' => NULL
46+
),
4247
'fetched' => array(
4348
'type' => 'datetime',
4449
'null' => true,

‎models/queued_task.php‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ class QueuedTask extends AppModel {
1818
* @param array $data any array
1919
* @return bool success
2020
*/
21-
public function createJob($jobName, $data) {
22-
return ($this->save($this->create(array(
21+
public function createJob($jobName, $data, $notBefore = null) {
22+
23+
$data = array(
2324
'jobtype' => $jobName,
2425
'data' => serialize($data)
25-
))));
26+
);
27+
if ($notBefore != null) {
28+
$data['notbefore'] = date('Y-m-d H:i:s', strtotime($notBefore));
29+
}
30+
return ($this->save($this->create($data)));
2631
}
2732

2833
/**
@@ -43,9 +48,19 @@ public function requestJob($capabilities) {
4348
foreach ($capabilities as $task) {
4449
$findConf['conditions']['OR'][] = array(
4550
'jobtype' => str_replace('queue_', '', $task['name']),
46-
'OR' => array(
47-
'fetched <' => date('Y-m-d H:i:s', time() - $task['timeout']),
48-
'fetched' => null
51+
'AND' => array(
52+
array(
53+
'OR' => array(
54+
'notbefore <' => date('Y-m-d H:i:s'),
55+
'notbefore' => null
56+
)
57+
),
58+
array(
59+
'OR' => array(
60+
'fetched <' => date('Y-m-d H:i:s', time() - $task['timeout']),
61+
'fetched' => null
62+
)
63+
)
4964
),
5065
'failed <' => ($task['retries'] + 1)
5166
);

‎tests/cases/models/queued_task.test.php‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function start() {
4444
/**
4545
* Basic Instance test
4646
*/
47-
public function testCountryInstance() {
47+
public function testQueueInstance() {
4848
$this->assertTrue(is_a($this->QueuedTask, 'TestQueuedTask'));
4949
}
5050

@@ -178,5 +178,16 @@ public function testSequence() {
178178
$this->assertEqual(9 - $num, $this->QueuedTask->getLength());
179179
}
180180
}
181+
182+
public function testNotBefore() {
183+
$this->assertTrue($this->QueuedTask->createJob('task1', null, '+ 1 Min'));
184+
$this->assertTrue($this->QueuedTask->createJob('task1', null, '+ 1 Day'));
185+
$this->assertTrue($this->QueuedTask->createJob('task1', null, '2009-07-01 12:00:00'));
186+
$data = $this->QueuedTask->find('all');
187+
$this->assertEqual($data[0]['TestQueuedTask']['notbefore'], date('Y-m-d H:i:s', strtotime('+ 1 Min')));
188+
$this->assertEqual($data[1]['TestQueuedTask']['notbefore'], date('Y-m-d H:i:s', strtotime('+ 1 Day')));
189+
$this->assertEqual($data[2]['TestQueuedTask']['notbefore'], '2009-07-01 12:00:00');
190+
191+
}
181192
}
182193
?>

‎tests/fixtures/queued_task_fixture.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class QueuedTaskFixture extends CakeTestFixture {
3333
'type' => 'datetime',
3434
'null' => false
3535
),
36+
'notbefore' => array(
37+
'type' => 'datetime',
38+
'null' => false,
39+
'default' => NULL
40+
),
3641
'fetched' => array(
3742
'type' => 'datetime',
3843
'null' => true,

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL