Skip to content

Commit 3de1010

Browse files
authored
Merge pull request #42 from Oefenweb/force-data-to-be-array
Force data to be an array
2 parents 2b788c8 + 8ddd60e commit 3de1010

3 files changed

Lines changed: 23 additions & 23 deletions

File tree

Console/Command/Task/QueueExampleTask.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function add() {
6363
$this->out(' ');
6464

6565
// Adding a task of type 'example' with no additionally passed data
66-
if ($this->QueuedTask->createJob('Example', null)) {
66+
if ($this->QueuedTask->createJob('Example', [])) {
6767
$this->out(__d('queue', 'OK, job created, now run the worker'));
6868
} else {
6969
$this->err(__d('queue', 'Could not create Job'));
@@ -76,10 +76,10 @@ public function add() {
7676
* This function is executed, when a worker is executing a task.
7777
* The return parameter will determine, if the task will be marked completed, or be requeued.
7878
*
79-
* @param mixed $data Job data (passed on creation)
79+
* @param array $data Job data (passed on creation)
8080
* @return bool Success
8181
*/
82-
public function run($data) {
82+
public function run(array $data) : bool {
8383
$this->hr();
8484
$this->out(__d('queue', 'CakePHP Queue Example task.'));
8585
$this->hr();

Model/QueuedTask.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ class QueuedTask extends AppModel {
1818
* Adds a new Job to the queue.
1919
*
2020
* @param string $taskName A queue task name
21-
* @param mixed $data Any data
22-
* @param string $notBefore A datetime which indicates when the job may be executed
21+
* @param array $data Any data
22+
* @param ?string $notBefore A datetime which indicates when the job may be executed
2323
* @return mixed On success `Model::$data` if its not empty or true, false on failure
2424
*/
25-
public function createJob($taskName, $data, $notBefore = null) {
25+
public function createJob(string $taskName, array $data, $notBefore = null) {
2626
$data = [
2727
'task' => $taskName,
2828
'data' => serialize($data),

Test/Case/Model/QueuedTaskTest.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ public function testSequence() {
183183
* @return void
184184
*/
185185
public function testNotBefore() {
186-
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', null, '+ 1 Min'));
187-
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', null, '+ 1 Day'));
188-
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', null, '2009-07-01 12:00:00'));
186+
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '+ 1 Min'));
187+
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '+ 1 Day'));
188+
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', [], '2009-07-01 12:00:00'));
189189
$data = $this->QueuedTask->find('all');
190190
$this->assertEqual($data[4]['QueuedTask']['not_before'], date('Y-m-d H:i:s', strtotime('+ 1 Min')));
191191
$this->assertEqual($data[5]['QueuedTask']['not_before'], date('Y-m-d H:i:s', strtotime('+ 1 Day')));
@@ -212,35 +212,35 @@ public function testNotBeforeOrder() {
212212
'retries' => 2
213213
]
214214
];
215-
$this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', null));
216-
$this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', null));
215+
$this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', []));
216+
$this->assertTrue((bool)$this->QueuedTask->createJob('dummytask', []));
217217
// Create a task with it's execution target some seconds in the past, so it should jump to the top of the list
218-
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', 'three', '- 3 Seconds'));
219-
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', 'two', '- 4 Seconds'));
220-
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', 'one', '- 5 Seconds'));
218+
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['three'], '- 3 Seconds'));
219+
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['two'], '- 4 Seconds'));
220+
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['one'], '- 5 Seconds'));
221221

222222
// When usin requestJob, the jobs we just created should be delivered in this order,
223223
// NOT the order in which they where created
224224
$expected = [
225225
[
226226
'name' => 'task1',
227-
'data' => 'one'
227+
'data' => ['one']
228228
],
229229
[
230230
'name' => 'task1',
231-
'data' => 'two'
231+
'data' => ['two']
232232
],
233233
[
234234
'name' => 'task1',
235-
'data' => 'three'
235+
'data' => ['three']
236236
],
237237
[
238238
'name' => 'dummytask',
239-
'data' => ''
239+
'data' => []
240240
],
241241
[
242242
'name' => 'dummytask',
243-
'data' => ''
243+
'data' => []
244244
]
245245
];
246246

@@ -265,15 +265,15 @@ public function testRequeueAfterTimeout() {
265265
]
266266
];
267267

268-
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', '1'));
268+
$this->assertTrue((bool)$this->QueuedTask->createJob('task1', ['1']));
269269
$tmp = $this->QueuedTask->requestJob($capabilities);
270270
$this->assertEqual($tmp['task'], 'task1');
271-
$this->assertEqual(unserialize($tmp['data']), '1');
271+
$this->assertEqual(unserialize($tmp['data']), ['1']);
272272
$this->assertEqual($tmp['failed_count'], '0');
273273
sleep(2);
274274
$tmp = $this->QueuedTask->requestJob($capabilities);
275275
$this->assertEqual($tmp['task'], 'task1');
276-
$this->assertEqual(unserialize($tmp['data']), '1');
276+
$this->assertEqual(unserialize($tmp['data']), ['1']);
277277
$this->assertEqual($tmp['failed_count'], '1');
278278
$this->assertEqual($tmp['failure_message'], 'Restart after timeout');
279279
}
@@ -284,7 +284,7 @@ public function testRequeueAfterTimeout() {
284284
* @return void
285285
*/
286286
public function testMarkJobFailed() {
287-
$this->QueuedTask->createJob('dummytask', null);
287+
$this->QueuedTask->createJob('dummytask', []);
288288
$id = $this->QueuedTask->id;
289289
$expected = 'Timeout: 100';
290290
$this->QueuedTask->markJobFailed($id, $expected);

0 commit comments

Comments
 (0)