/** * Example add functionality. * Will create one example job in the queue, which later will be executed using run(); * * @return void */ public function add() { $this->out('CakePHP Queue Retry Example task.'); $this->hr(); $this->out('This is a very simple example of a QueueTask and how retries work.'); $this->out('I will now add an example Job into the Queue.'); $this->out('This job will only produce some console output on the worker that it runs on.'); $this->out(' '); $this->out('To run a Worker use:'); $this->out(' cake Queue.Queue runworker'); $this->out(' '); $this->out('You can find the sourcecode of this task in: '); $this->out(__FILE__); $this->out(' '); file_put_contents($this->file, '0'); /* * Adding a task of type 'example' with no additionally passed data */ if ($this->QueuedTasks->createJob('RetryExample', null)) { $this->out('OK, job created, now run the worker'); } else { $this->err('Could not create Job'); } }
/** * Testing request grouping. * * @return void */ public function testRequestGroup() { $capabilities = ['task1' => ['name' => 'task1', 'timeout' => 1, 'retries' => 2, 'rate' => 0]]; // create an ungrouped task $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', 1)); //create a Grouped Task $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', 2, null, 'testgroup')); // Fetching without group should completely ignore the Group field. $this->QueuedTasks->clearKey(); $tmp = $this->QueuedTasks->requestJob($capabilities); $this->assertEquals('task1', $tmp['jobtype']); $this->assertEquals(1, unserialize($tmp['data'])); $this->QueuedTasks->clearKey(); $tmp = $this->QueuedTasks->requestJob($capabilities); $this->assertEquals('task1', $tmp['jobtype']); $this->assertEquals(2, unserialize($tmp['data'])); // well, lets tra that Again, while limiting by Group // create an ungrouped task $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', 3)); //create a Grouped Task $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', 4, null, 'testgroup', 'Job number 4')); $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', 5, null, null, 'Job number 5')); $this->assertTrue((bool) $this->QueuedTasks->createJob('task1', 6, null, 'testgroup', 'Job number 6')); // we should only get tasks 4 and 6, in that order, when requesting inside the group $this->QueuedTasks->clearKey(); $tmp = $this->QueuedTasks->requestJob($capabilities, 'testgroup'); $this->assertEquals('task1', $tmp['jobtype']); $this->assertEquals(4, unserialize($tmp['data'])); $this->QueuedTasks->clearKey(); $tmp = $this->QueuedTasks->requestJob($capabilities, 'testgroup'); $this->assertEquals('task1', $tmp['jobtype']); $this->assertEquals(6, unserialize($tmp['data'])); // use FindProgress on the testgroup: $progress = $this->QueuedTasks->find('all', ['conditions' => ['task_group' => 'testgroup']])->toArray(); $this->assertEquals(3, count($progress)); $this->assertNull($progress[0]['reference']); #$this->assertEquals($progress[0]['status'], 'IN_PROGRESS'); $this->assertEquals('Job number 4', $progress[1]['reference']); #$this->assertEquals($progress[1]['status'], 'IN_PROGRESS'); $this->assertEquals('Job number 6', $progress[2]['reference']); #$this->assertEquals($progress[2]['status'], 'IN_PROGRESS'); }