Skip to content
Browse files

refactor code and more tests for new methods

  • Loading branch information...
1 parent 954c8b2 commit 8da04824ba1eaefd0705c6b9a8a179ad818fbe8b @dogmatic69 committed Mar 27, 2013
View
16 Controller/InfinitasJobsAppController.php
@@ -75,4 +75,20 @@ public function edit($id = null) {
}
}
+/**
+ * delete a record
+ *
+ * @param string $id the record to delete
+ * @return void
+ */
+ public function delete($id) {
+ if ($this->{$this->modelClass}->delete($id)) {
+ $this->Session->setFlash($this->modelclass . ' was deleted');
+ } else {
+ $this->Session->setFlash($this->modelclass . ' could not be deleted');
+ }
+
+ $this->redirect($this->referer());
+ }
+
}
View
60 Controller/InfinitasJobsController.php
@@ -38,10 +38,14 @@ public function index() {
$this->modelClass . '.completed' => null,
$this->modelClass . '.locked' => null,
$this->modelClass . '.failed' => null
+ ),
+ 'contain' => array(
+ 'InfinitasJobLog',
+ 'InfinitasJobQueue'
)
);
-
- $this->_jobsIndex();
+ $infinitasJobs = $this->Paginator->paginate();
+ $this->set(compact('infinitasJobs'));
}
/**
@@ -50,15 +54,8 @@ public function index() {
* @return void
*/
public function locked() {
- $this->Paginator->settings = array(
- 'conditions' => array(
- $this->modelClass . '.locked IS NOT NULL',
- $this->modelClass . '.completed IS NULL',
- $this->modelClass . '.failed IS NULL',
- )
- );
-
- $this->_jobsIndex();
+ $infinitasJobs = $this->Paginator->paginate('locked');
+ $this->set(compact('infinitasJobs'));
}
/**
@@ -67,16 +64,8 @@ public function locked() {
* @return void
*/
public function failed() {
- $this->Paginator->settings = array(
- 'conditions' => array(
- $this->modelClass . '.failed IS NOT NULL'
- ),
- 'contain' => array(
- 'InfinitasJobError'
- )
- );
-
- $this->_jobsIndex();
+ $infinitasJobs = $this->Paginator->paginate('failed');
+ $this->set(compact('infinitasJobs'));
}
/**
@@ -85,33 +74,8 @@ public function failed() {
* @return void
*/
public function completed() {
- $this->Paginator->settings = array(
- 'conditions' => array(
- $this->modelClass . '.completed IS NOT NULL'
- )
- );
-
- $this->_jobsIndex();
- }
-
-/**
- * @brief DRY method used for the various index methods
- *
- * @see InfinitasJobsController::index()
- * @see InfinitasJobsController::locked()
- * @see InfinitasJobsController::failed()
- * @see InfinitasJobsController::completed()
- *
- * @return void
- */
- protected function _jobsIndex() {
- if(empty($this->Paginator->settings['contain'])) {
- $this->Paginator->settings['contain'] = array();
- }
- $this->Paginator->settings['contain'][] = 'InfinitasJobLog';
- $this->Paginator->settings['contain'][] = 'InfinitasJobQueue';
- $infinitasJobs = $this->Paginator->paginate(null);
-
+ $infinitasJobs = $this->Paginator->paginate('completed');
$this->set(compact('infinitasJobs'));
}
+
}
View
126 Model/InfinitasJob.php
@@ -31,7 +31,11 @@ class InfinitasJob extends InfinitasJobsAppModel {
'handler' => true,
'attempts' => true,
'status' => true,
- 'max_attempts' => true
+ 'max_attempts' => true,
+ 'pending' => true,
+ 'locked' => true,
+ 'failed' => true,
+ 'completed' => true
);
/**
@@ -605,4 +609,124 @@ protected function _findMax_attempts($state, $query = array(), $results = array(
return 0;
}
+
+/**
+ * Find pending jobs
+ *
+ * @param string $state the state of the find
+ * @param array $query the query conditions
+ * @param array $results the query results
+ *
+ * @return void
+ */
+ protected function _findPending($state, $query, $results = array()) {
+ if ($state == 'before') {
+ $query['conditions'] = array_merge((array)$query['conditions'], array(
+ $this->alias . '.locked IS NULL',
+ $this->alias . '.completed IS NULL',
+ $this->alias . '.failed IS NULL',
+ $this->alias . '.attempts' => 0,
+ ));
+
+ if (empty($query['contain'])) {
+ $query['contain'] = array();
+ }
+ $query['contain'] = array_merge((array)$query['contain'], array(
+ 'InfinitasJobLog',
+ 'InfinitasJobQueue'
+ ));
+ return $query;
+ }
+
+ return $results;
+ }
+
+/**
+ * Find locked jobs
+ *
+ * @param string $state the state of the find
+ * @param array $query the query conditions
+ * @param array $results the query results
+ *
+ * @return void
+ */
+ protected function _findLocked($state, $query, $results = array()) {
+ if ($state == 'before') {
+ $query['conditions'] = array_merge((array)$query['conditions'], array(
+ $this->alias . '.locked IS NOT NULL',
+ $this->alias . '.completed IS NULL',
+ $this->alias . '.failed IS NULL',
+ ));
+
+ if (empty($query['contain'])) {
+ $query['contain'] = array();
+ }
+ $query['contain'] = array_merge((array)$query['contain'], array(
+ 'InfinitasJobLog',
+ 'InfinitasJobQueue'
+ ));
+ return $query;
+ }
+
+ return $results;
+ }
+
+/**
+ * Find failed jobs
+ *
+ * @param string $state the state of the find
+ * @param array $query the query conditions
+ * @param array $results the query results
+ *
+ * @return void
+ */
+ protected function _findFailed($state, $query, $results = array()) {
+ if ($state == 'before') {
+ $query['conditions'] = array_merge((array)$query['conditions'], array(
+ $this->alias . '.failed IS NOT NULL'
+ ));
+
+ if (empty($query['contain'])) {
+ $query['contain'] = array();
+ }
+ $query['contain'] = array_merge((array)$query['contain'], array(
+ 'InfinitasJobError',
+ 'InfinitasJobLog',
+ 'InfinitasJobQueue'
+ ));
+
+ return $query;
+ }
+
+ return $results;
+ }
+
+/**
+ * Find locked jobs
+ *
+ * @param string $state the state of the find
+ * @param array $query the query conditions
+ * @param array $results the query results
+ *
+ * @return void
+ */
+ protected function _findCompleted($state, $query, $results = array()) {
+ if ($state == 'before') {
+ $query['conditions'] = array_merge((array)$query['conditions'], array(
+ $this->alias . '.completed IS NOT NULL'
+ ));
+
+ if (empty($query['contain'])) {
+ $query['contain'] = array();
+ }
+ $query['contain'] = array_merge((array)$query['contain'], array(
+ 'InfinitasJobLog',
+ 'InfinitasJobQueue'
+ ));
+
+ return $query;
+ }
+
+ return $results;
+ }
}
View
3 Model/InfinitasJobsAppModel.php
@@ -6,7 +6,8 @@
class InfinitasJobsAppModel extends AppModel {
public $actsAs = array(
- 'InfinitasJobs.BigData'
+ 'InfinitasJobs.BigData',
+ 'Containable'
);
public $recursive = -1;
View
2 Test/Case/Model/InfinitasJobQueueTest.php
@@ -65,7 +65,7 @@ public function testFindIdFromSlug() {
));
$result = $this->InfinitasJobQueue->find('idFromSlug', 'queue*');
- $this->assertEquals('queue-1', $result);
+ $this->assertEquals('queue-3', $result);
$result = $this->InfinitasJobQueue->find('idFromSlug', 'foobar');
$this->assertFalse($result);
View
183 Test/Case/Model/InfinitasJobTest.php
@@ -381,4 +381,187 @@ public function testGetHandler() {
$this->assertFalse($this->InfinitasJob->find('handler', 'fake'));
}
+
+ public function testCustomFindMethods() {
+ $this->_customFindMethodData();
+ $conditions = array(
+ 'conditions' => array(
+ 'InfinitasJob.infinitas_job_queue_id' => 'queue-3'
+ )
+ );
+ $defaultData = array(
+ 'InfinitasJobQueue' => array(
+ 'id' => 'queue-3',
+ 'name' => 'Queue3',
+ 'slug' => 'queue3',
+ 'max_attempts' => 5,
+ 'sleep_between' => 5,
+ 'pending_job_count' => 1,
+ 'failed_job_count' => 1,
+ 'locked_job_count' => 1,
+ 'completed_job_count' => 1,
+ 'pid' => null,
+ 'created' => '2012-08-13 13:43:56',
+ 'modified' => '2012-08-13 13:43:56',
+ 'retry_delay' => 7200
+ ),
+ 'InfinitasJobLog' => array(
+ ),
+ );
+
+ $expected = array_merge($defaultData, array(
+ 'InfinitasJob' => array(
+ 'id' => '3-pending',
+ 'infinitas_job_queue_id' => 'queue-3',
+ 'host' => null,
+ 'pid' => null,
+ 'handler' => 'O:21:"InfinitasJobs_TestJob":1:{s:10:"_internals";a:0:{}}',
+ 'attempts' => 0,
+ 'run_at' => '2030-03-22 16:22:20',
+ 'completed' => null,
+ 'locked' => null,
+ 'failed' => null,
+ 'infinitas_job_error_count' => 0,
+ 'created' => '2013-03-22 16:22:20',
+ 'modified' => '2013-03-22 16:22:20'
+ )
+ ));
+ $result = $this->InfinitasJob->find('pending', $conditions);
+ $this->assertTrue(count($result) === 1);
+ $this->assertEquals($expected, $result[0]);
+
+ $expected = array_merge($defaultData, array(
+ 'InfinitasJob' => array(
+ 'id' => '3-complete',
+ 'infinitas_job_queue_id' => 'queue-3',
+ 'host' => null,
+ 'pid' => null,
+ 'handler' => 'O:21:"InfinitasJobs_TestJob":1:{s:10:"_internals";a:0:{}}',
+ 'attempts' => 0,
+ 'run_at' => '2012-03-22 16:22:20',
+ 'completed' => '2012-03-22 16:22:20',
+ 'locked' => null,
+ 'failed' => null,
+ 'infinitas_job_error_count' => 0,
+ 'created' => '2013-03-22 16:22:20',
+ 'modified' => '2013-03-22 16:22:20'
+ )
+ ));
+ $result = $this->InfinitasJob->find('completed', $conditions);
+ $this->assertTrue(count($result) === 1);
+ $this->assertEquals($expected, $result[0]);
+
+ $expected = array_merge($defaultData, array(
+ 'InfinitasJob' => array(
+ 'id' => '3-locked',
+ 'infinitas_job_queue_id' => 'queue-3',
+ 'host' => null,
+ 'pid' => null,
+ 'handler' => 'O:21:"InfinitasJobs_TestJob":1:{s:10:"_internals";a:0:{}}',
+ 'attempts' => 0,
+ 'run_at' => '2012-03-22 16:22:20',
+ 'completed' => null,
+ 'locked' => '2012-03-22 16:22:20',
+ 'failed' => null,
+ 'infinitas_job_error_count' => 0,
+ 'created' => '2013-03-22 16:22:20',
+ 'modified' => '2013-03-22 16:22:20'
+ )
+ ));
+ $result = $this->InfinitasJob->find('locked', $conditions);
+ $this->assertTrue(count($result) === 1);
+ $this->assertEquals($expected, $result[0]);
+
+ $expected = array_merge($defaultData, array(
+ 'InfinitasJob' => array(
+ 'id' => '3-failed',
+ 'infinitas_job_queue_id' => 'queue-3',
+ 'host' => null,
+ 'pid' => null,
+ 'handler' => 'O:21:"InfinitasJobs_TestJob":1:{s:10:"_internals";a:0:{}}',
+ 'attempts' => 0,
+ 'run_at' => '2012-03-22 16:22:20',
+ 'completed' => null,
+ 'locked' => null,
+ 'failed' => '2012-03-22 16:22:20',
+ 'infinitas_job_error_count' => 1,
+ 'created' => '2013-03-22 16:22:20',
+ 'modified' => '2013-03-22 16:22:20'
+ ),
+ 'InfinitasJobError' => array(
+
+ )
+ ));
+ $result = $this->InfinitasJob->find('failed', $conditions);
+ $this->assertTrue(count($result) === 1);
+ $this->assertEquals($expected, $result[0]);
+ }
+
+ protected function _customFindMethodData() {
+ $data = array(
+ array(
+ 'id' => '3-pending',
+ 'infinitas_job_queue_id' => 'queue-3',
+ 'host' => null,
+ 'pid' => null,
+ 'handler' => 'O:21:"InfinitasJobs_TestJob":1:{s:10:"_internals";a:0:{}}',
+ 'attempts' => 0,
+ 'run_at' => '2030-03-22 16:22:20',
+ 'completed' => null,
+ 'locked' => null,
+ 'failed' => null,
+ 'infinitas_job_error_count' => 0,
+ 'created' => '2013-03-22 16:22:20',
+ 'modified' => '2013-03-22 16:22:20'
+ ),
+ array(
+ 'id' => '3-complete',
+ 'infinitas_job_queue_id' => 'queue-3',
+ 'host' => null,
+ 'pid' => null,
+ 'handler' => 'O:21:"InfinitasJobs_TestJob":1:{s:10:"_internals";a:0:{}}',
+ 'attempts' => 0,
+ 'run_at' => '2012-03-22 16:22:20',
+ 'completed' => '2012-03-22 16:22:20',
+ 'locked' => null,
+ 'failed' => null,
+ 'infinitas_job_error_count' => 0,
+ 'created' => '2013-03-22 16:22:20',
+ 'modified' => '2013-03-22 16:22:20'
+ ),
+ array(
+ 'id' => '3-locked',
+ 'infinitas_job_queue_id' => 'queue-3',
+ 'host' => null,
+ 'pid' => null,
+ 'handler' => 'O:21:"InfinitasJobs_TestJob":1:{s:10:"_internals";a:0:{}}',
+ 'attempts' => 0,
+ 'run_at' => '2012-03-22 16:22:20',
+ 'completed' => null,
+ 'locked' => '2012-03-22 16:22:20',
+ 'failed' => null,
+ 'infinitas_job_error_count' => 0,
+ 'created' => '2013-03-22 16:22:20',
+ 'modified' => '2013-03-22 16:22:20'
+ ),
+ array(
+ 'id' => '3-failed',
+ 'infinitas_job_queue_id' => 'queue-3',
+ 'host' => null,
+ 'pid' => null,
+ 'handler' => 'O:21:"InfinitasJobs_TestJob":1:{s:10:"_internals";a:0:{}}',
+ 'attempts' => 0,
+ 'run_at' => '2012-03-22 16:22:20',
+ 'completed' => null,
+ 'locked' => null,
+ 'failed' => '2012-03-22 16:22:20',
+ 'infinitas_job_error_count' => 1,
+ 'created' => '2013-03-22 16:22:20',
+ 'modified' => '2013-03-22 16:22:20'
+ )
+ );
+
+ $saved = $this->InfinitasJob->saveAll($data);
+ $this->assertTrue($saved);
+ }
}
View
2 Test/Fixture/InfinitasJobFixture.php
@@ -73,6 +73,6 @@ class InfinitasJobFixture extends CakeTestFixture {
'infinitas_job_error_count' => 0,
'created' => '2013-03-22 16:22:20',
'modified' => '2013-03-22 16:22:20'
- ),
+ )
);
}
View
15 Test/Fixture/InfinitasJobQueueFixture.php
@@ -60,5 +60,20 @@ class InfinitasJobQueueFixture extends CakeTestFixture {
'modified' => '2012-08-13 13:43:56',
'retry_delay' => 7200
),
+ array(
+ 'id' => 'queue-3',
+ 'name' => 'Queue3',
+ 'slug' => 'queue3',
+ 'max_attempts' => 5,
+ 'sleep_between' => 5,
+ 'pending_job_count' => 1,
+ 'failed_job_count' => 1,
+ 'locked_job_count' => 1,
+ 'completed_job_count' => 1,
+ 'pid' => null,
+ 'created' => '2012-08-13 13:43:56',
+ 'modified' => '2012-08-13 13:43:56',
+ 'retry_delay' => 7200
+ ),
);
}

0 comments on commit 8da0482

Please sign in to comment.
Something went wrong with that request. Please try again.