diff --git a/Console/Command/Task/QueueExampleTask.php b/Console/Command/Task/QueueExampleTask.php index 882c3e1..4de2bff 100644 --- a/Console/Command/Task/QueueExampleTask.php +++ b/Console/Command/Task/QueueExampleTask.php @@ -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')); @@ -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(); diff --git a/Model/QueuedTask.php b/Model/QueuedTask.php index 5d0540b..7825df2 100644 --- a/Model/QueuedTask.php +++ b/Model/QueuedTask.php @@ -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), diff --git a/Test/Case/Model/QueuedTaskTest.php b/Test/Case/Model/QueuedTaskTest.php index ea32035..dab417f 100644 --- a/Test/Case/Model/QueuedTaskTest.php +++ b/Test/Case/Model/QueuedTaskTest.php @@ -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'))); @@ -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' => [] ] ]; @@ -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'); } @@ -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);