From d1650c6f3272e48730e5563a48e9782508479349 Mon Sep 17 00:00:00 2001 From: MGriesbach Date: Sat, 1 May 2010 13:02:09 +0200 Subject: [PATCH] Switched Codeformatter to ZendStudio 7.2.x No Content Changes. --- config/sql/queue.php | 2 +- models/queued_task.php | 12 +++--- tests/cases/models/queued_task.test.php | 54 ++++++++++++------------- vendors/shells/queue.php | 18 ++++----- vendors/shells/tasks/queue_email.php | 3 -- vendors/shells/tasks/queue_example.php | 4 +- 6 files changed, 45 insertions(+), 48 deletions(-) diff --git a/config/sql/queue.php b/config/sql/queue.php index 07ac14c..3e61f53 100644 --- a/config/sql/queue.php +++ b/config/sql/queue.php @@ -16,7 +16,7 @@ function before($event = array()) { function after($event = array()) { } - + public $queued_tasks = array( 'id' => array( 'type' => 'integer', diff --git a/models/queued_task.php b/models/queued_task.php index d543877..a7dae96 100644 --- a/models/queued_task.php +++ b/models/queued_task.php @@ -8,11 +8,11 @@ * @link http://github.com/MSeven/cakephp_queue */ class QueuedTask extends AppModel { - + public $name = 'QueuedTask'; - + public $rateHistory = array(); - + public $exit = false; /** @@ -23,7 +23,7 @@ class QueuedTask extends AppModel { * @return bool success */ public function createJob($jobName, $data, $notBefore = null) { - + $data = array( 'jobtype' => $jobName, 'data' => serialize($data) @@ -47,7 +47,7 @@ public function onError() { public function requestJob($capabilities) { $idlist = array(); $wasFetched = array(); - + $findConf = array( 'conditions' => array( 'completed' => null, @@ -219,7 +219,7 @@ public function cleanOldJobs() { $this->deleteAll(array( 'completed < ' => date('Y-m-d H:i:s', time() - Configure::read('queue.cleanuptimeout')) )); - + } } diff --git a/tests/cases/models/queued_task.test.php b/tests/cases/models/queued_task.test.php index 06bc316..c4b7811 100644 --- a/tests/cases/models/queued_task.test.php +++ b/tests/cases/models/queued_task.test.php @@ -54,16 +54,16 @@ public function testQueueInstance() { public function testCreateAndCount() { // at first, the queue should contain 0 items. $this->assertEqual(0, $this->QueuedTask->getLength()); - + // create a job $this->assertTrue($this->QueuedTask->createJob('test1', array( 'some' => 'random', 'test' => 'data' ))); - + // test if queue Length is 1 now. $this->assertEqual(1, $this->QueuedTask->getLength()); - + //create some more jobs $this->assertTrue($this->QueuedTask->createJob('test2', array( 'some' => 'random', @@ -77,10 +77,10 @@ public function testCreateAndCount() { 'some' => 'random', 'test' => 'data4' ))); - + //overall queueLength shpould now be 4 $this->assertEqual(4, $this->QueuedTask->getLength()); - + // there should be 1 task of type 'test1', one of type 'test3' and 2 of type 'test2' $this->assertEqual(1, $this->QueuedTask->getLength('test1')); $this->assertEqual(2, $this->QueuedTask->getLength('test2')); @@ -103,7 +103,7 @@ public function testCreateAndFetch() { 'x4' => 'y4' ); // start off empty. - + $this->assertEqual(array(), $this->QueuedTask->find('all')); // at first, the queue should contain 0 items. @@ -112,7 +112,7 @@ public function testCreateAndFetch() { $this->assertFalse($this->QueuedTask->requestJob($capabilities)); // insert one job. $this->assertTrue($this->QueuedTask->createJob('task1', $testData)); - + // fetch and check the first job. $data = $this->QueuedTask->requestJob($capabilities); $this->assertEqual(1, $data['id']); @@ -120,13 +120,13 @@ public function testCreateAndFetch() { $this->assertEqual(0, $data['failed']); $this->assertNull($data['completed']); $this->assertEqual($testData, unserialize($data['data'])); - + // after this job has been fetched, it may not be reassigned. $this->assertEqual(array(), $this->QueuedTask->requestJob($capabilities)); - + // queue length is still 1 since the first job did not finish. $this->assertEqual(1, $this->QueuedTask->getLength()); - + // Now mark Task1 as done $this->assertTrue($this->QueuedTask->markJobDone(1)); // Should be 0 again. @@ -156,7 +156,7 @@ public function testSequence() { } // 10 jobs in the queue. $this->assertEqual(10, $this->QueuedTask->getLength()); - + // jobs should be fetched in the original sequence. foreach (range(0, 4) as $num) { $job = $this->QueuedTask->requestJob($capabilities); @@ -168,7 +168,7 @@ public function testSequence() { $this->assertTrue($this->QueuedTask->markJobDone($num + 1)); $this->assertEqual(9 - $num, $this->QueuedTask->getLength()); } - + // jobs should be fetched in the original sequence. foreach (range(5, 9) as $num) { $job = $this->QueuedTask->requestJob($capabilities); @@ -217,7 +217,7 @@ public function testNotBeforeOrder() { $this->assertTrue($this->QueuedTask->createJob('task1', 'three', '- 3 Seconds')); $this->assertTrue($this->QueuedTask->createJob('task1', 'two', '- 4 Seconds')); $this->assertTrue($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 = array( array( @@ -241,7 +241,7 @@ public function testNotBeforeOrder() { 'data' => '' ) ); - + foreach ($expected as $item) { $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($item['name'], $tmp['jobtype']); @@ -267,10 +267,10 @@ public function testRateLimit() { 'retries' => 2 ) ); - + // clear out the rate history $this->QueuedTask->rateHistory = array(); - + $this->assertTrue($this->QueuedTask->createJob('task1', '1')); $this->assertTrue($this->QueuedTask->createJob('task1', '2')); $this->assertTrue($this->QueuedTask->createJob('task1', '3')); @@ -278,48 +278,48 @@ public function testRateLimit() { $this->assertTrue($this->QueuedTask->createJob('dummytask', null)); $this->assertTrue($this->QueuedTask->createJob('dummytask', null)); $this->assertTrue($this->QueuedTask->createJob('dummytask', null)); - + //At first we get task1-1. $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($tmp['jobtype'], 'task1'); $this->assertEqual(unserialize($tmp['data']), '1'); - + //The rate limit should now skip over task1-2 and fetch a dummytask. $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($tmp['jobtype'], 'dummytask'); $this->assertEqual(unserialize($tmp['data']), null); - + //and again. $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($tmp['jobtype'], 'dummytask'); $this->assertEqual(unserialize($tmp['data']), null); - + //Then some time passes sleep(1); - + //Now we should get task1-2 $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($tmp['jobtype'], 'task1'); $this->assertEqual(unserialize($tmp['data']), '2'); - + //and again rate limit to dummytask. $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($tmp['jobtype'], 'dummytask'); $this->assertEqual(unserialize($tmp['data']), null); - + //Then some more time passes sleep(1); - + //Now we should get task1-3 $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($tmp['jobtype'], 'task1'); $this->assertEqual(unserialize($tmp['data']), '3'); - + //and again rate limit to dummytask. $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($tmp['jobtype'], 'dummytask'); $this->assertEqual(unserialize($tmp['data']), null); - + //and now the queue is empty $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($tmp, null); @@ -334,7 +334,7 @@ public function testRequeueAfterTimeout() { 'rate' => 0 ) ); - + $this->assertTrue($this->QueuedTask->createJob('task1', '1')); $tmp = $this->QueuedTask->requestJob($capabilities); $this->assertEqual($tmp['jobtype'], 'task1'); diff --git a/vendors/shells/queue.php b/vendors/shells/queue.php index 4d9b4a6..3a9b99a 100644 --- a/vendors/shells/queue.php +++ b/vendors/shells/queue.php @@ -17,7 +17,7 @@ class queueShell extends Shell { * @var QueuedTask */ public $QueuedTask; - + private $taskConf; /** @@ -26,7 +26,7 @@ class queueShell extends Shell { public function initialize() { App::import('Folder'); $this->_loadModels(); - + foreach ($this->Dispatch->shellPaths as $path) { $folder = new Folder($path . DS . 'tasks'); $this->tasks = array_merge($this->tasks, $folder->find('queue_.*\.php')); @@ -35,10 +35,10 @@ public function initialize() { foreach ($this->tasks as &$task) { $task = basename($task, '.php'); } - + //Config can be overwritten via local app config. Configure::load('queue'); - + $conf = Configure::read('queue'); if (!is_array($conf)) { $conf = array(); @@ -93,7 +93,7 @@ public function add() { $this->out('Please call like this:'); $this->out(' cake queue add '); } else { - + if (in_array($this->args[0], $this->taskNames)) { $this->{$this->args[0]}->add(); } elseif (in_array('queue_' . $this->args[0], $this->taskNames)) { @@ -116,7 +116,7 @@ public function add() { public function runworker() { $exit = false; $starttime = time(); - + while (!$exit) { $this->out('Looking for Job....'); $data = $this->QueuedTask->requestJob($this->getTaskConf()); @@ -138,7 +138,7 @@ public function runworker() { $this->out('nothing to do, sleeping.'); sleep(Configure::read('queue.sleeptime')); } - + // check if we are over the maximum runtime and end processing if so. if (Configure::read('queue.workermaxruntime') != 0 && (time() - $starttime) >= Configure::read('queue.workermaxruntime')) { $exit = true; @@ -168,9 +168,9 @@ public function clean() { */ public function stats() { $this->out('Jobs currenty in the Queue:'); - + $types = $this->QueuedTask->getTypes(); - + foreach ($types as $type) { $this->out(" " . str_pad($type, 20, ' ', STR_PAD_RIGHT) . ": " . $this->QueuedTask->getLength($type)); } diff --git a/vendors/shells/tasks/queue_email.php b/vendors/shells/tasks/queue_email.php index efcad35..26fa1a4 100644 --- a/vendors/shells/tasks/queue_email.php +++ b/vendors/shells/tasks/queue_email.php @@ -1,5 +1,4 @@ '', 'layout' => 'default' ); - public $timeout = 120; public $retries = 0; /** @@ -37,7 +35,6 @@ class queueEmailTask extends Shell { * @var Controller */ public $Controller; - /** * EmailComponent * diff --git a/vendors/shells/tasks/queue_example.php b/vendors/shells/tasks/queue_example.php index a559204..94fb687 100644 --- a/vendors/shells/tasks/queue_example.php +++ b/vendors/shells/tasks/queue_example.php @@ -21,14 +21,14 @@ class queueExampleTask extends Shell { public $uses = array( 'Queue.QueuedTask' ); - + /** * ZendStudio Codecomplete Hint * * @var QueuedTask */ public $QueuedTask; - + /** * Timeout für run, after which the Task is reassigned to a new worker. *