Skip to content

Commit

Permalink
Merge pull request #42 from Oefenweb/force-data-to-be-array
Browse files Browse the repository at this point in the history
Force data to be an array
  • Loading branch information
tersmitten authored Nov 28, 2019
2 parents 2b788c8 + 8ddd60e commit 3de1010
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Console/Command/Task/QueueExampleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function add() {
$this->out(' ');

// Adding a task of type 'example' with no additionally passed data
if ($this->QueuedTask->createJob('Example', null)) {
if ($this->QueuedTask->createJob('Example', [])) {
$this->out(__d('queue', 'OK, job created, now run the worker'));
} else {
$this->err(__d('queue', 'Could not create Job'));
Expand All @@ -76,10 +76,10 @@ public function add() {
* This function is executed, when a worker is executing a task.
* The return parameter will determine, if the task will be marked completed, or be requeued.
*
* @param mixed $data Job data (passed on creation)
* @param array $data Job data (passed on creation)
* @return bool Success
*/
public function run($data) {
public function run(array $data) : bool {
$this->hr();
$this->out(__d('queue', 'CakePHP Queue Example task.'));
$this->hr();
Expand Down
6 changes: 3 additions & 3 deletions Model/QueuedTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class QueuedTask extends AppModel {
* Adds a new Job to the queue.
*
* @param string $taskName A queue task name
* @param mixed $data Any data
* @param string $notBefore A datetime which indicates when the job may be executed
* @param array $data Any data
* @param ?string $notBefore A datetime which indicates when the job may be executed
* @return mixed On success `Model::$data` if its not empty or true, false on failure
*/
public function createJob($taskName, $data, $notBefore = null) {
public function createJob(string $taskName, array $data, $notBefore = null) {
$data = [
'task' => $taskName,
'data' => serialize($data),
Expand Down
34 changes: 17 additions & 17 deletions Test/Case/Model/QueuedTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ public function testSequence() {
* @return void
*/
public function testNotBefore() {
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', null, '+ 1 Min'));
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', null, '+ 1 Day'));
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', null, '2009-07-01 12:00:00'));
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '+ 1 Min'));
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '+ 1 Day'));
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '2009-07-01 12:00:00'));
$data = $this->QueuedTask->find('all');
$this->assertEqual($data[4]['QueuedTask']['not_before'], date('Y-m-d H:i:s', strtotime('+ 1 Min')));
$this->assertEqual($data[5]['QueuedTask']['not_before'], date('Y-m-d H:i:s', strtotime('+ 1 Day')));
Expand All @@ -212,35 +212,35 @@ public function testNotBeforeOrder() {
'retries' => 2
]
];
$this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', null));
$this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', null));
$this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', []));
$this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', []));
// Create a task with it's execution target some seconds in the past, so it should jump to the top of the list
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', 'three', '- 3 Seconds'));
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', 'two', '- 4 Seconds'));
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', 'one', '- 5 Seconds'));
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['three'], '- 3 Seconds'));
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['two'], '- 4 Seconds'));
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['one'], '- 5 Seconds'));

// When usin requestJob, the jobs we just created should be delivered in this order,
// NOT the order in which they where created
$expected = [
[
'name' => 'task1',
'data' => 'one'
'data' => ['one']
],
[
'name' => 'task1',
'data' => 'two'
'data' => ['two']
],
[
'name' => 'task1',
'data' => 'three'
'data' => ['three']
],
[
'name' => 'dummytask',
'data' => ''
'data' => []
],
[
'name' => 'dummytask',
'data' => ''
'data' => []
]
];

Expand All @@ -265,15 +265,15 @@ public function testRequeueAfterTimeout() {
]
];

$this->assertTrue((bool)$this->QueuedTask->createJob('task1', '1'));
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['1']));
$tmp = $this->QueuedTask->requestJob($capabilities);
$this->assertEqual($tmp['task'], 'task1');
$this->assertEqual(unserialize($tmp['data']), '1');
$this->assertEqual(unserialize($tmp['data']), ['1']);
$this->assertEqual($tmp['failed_count'], '0');
sleep(2);
$tmp = $this->QueuedTask->requestJob($capabilities);
$this->assertEqual($tmp['task'], 'task1');
$this->assertEqual(unserialize($tmp['data']), '1');
$this->assertEqual(unserialize($tmp['data']), ['1']);
$this->assertEqual($tmp['failed_count'], '1');
$this->assertEqual($tmp['failure_message'], 'Restart after timeout');
}
Expand All @@ -284,7 +284,7 @@ public function testRequeueAfterTimeout() {
* @return void
*/
public function testMarkJobFailed() {
$this->QueuedTask->createJob('dummytask', null);
$this->QueuedTask->createJob('dummytask', []);
$id = $this->QueuedTask->id;
$expected = 'Timeout: 100';
$this->QueuedTask->markJobFailed($id, $expected);
Expand Down

0 comments on commit 3de1010

Please sign in to comment.