diff --git a/application/core/JQW_Controller.php b/application/core/JQW_Controller.php index 1b78a2a70..361efd998 100644 --- a/application/core/JQW_Controller.php +++ b/application/core/JQW_Controller.php @@ -49,10 +49,20 @@ abstract class JQW_Controller extends JOB_Controller /** * To get the oldest added job using the given job type + * NOTE: just a wrapper */ protected function getOldestJob($type) { - $jobs = $this->jobsqueuelib->getOldestJob($type); + return $this->getOldestJobs($type, 1); + } + + /** + * To get the oldest added jobs using the given job type + * It is eventually specify the maximum amount of jobs to be retrieved + */ + protected function getOldestJobs($type, $maxAmount) + { + $jobs = $this->jobsqueuelib->getOldestJobs($type, $maxAmount); // If an error occurred then log it in database if (isError($jobs)) $this->logError(getError($jobs), $type); @@ -131,12 +141,7 @@ abstract class JQW_Controller extends JOB_Controller */ protected function generateJobs($status, $input) { - $job = new stdClass(); - - $job->{JobsQueueLib::PROPERTY_STATUS} = $status; - $job->{JobsQueueLib::PROPERTY_INPUT} = $input; - - return array($job); + return JobsQueueLib::generateJobs($status, $input); } } diff --git a/application/libraries/JobsQueueLib.php b/application/libraries/JobsQueueLib.php index a65bb0d29..d264f7119 100644 --- a/application/libraries/JobsQueueLib.php +++ b/application/libraries/JobsQueueLib.php @@ -58,13 +58,16 @@ class JobsQueueLib /** * To get the oldest added jobs using the given job type + * It is eventually specify the maximum amount of jobs to be retrieved */ - public function getOldestJob($type) + public function getOldestJobs($type, $maxAmount = null) { $this->_ci->JobsQueueModel->resetQuery(); $this->_ci->JobsQueueModel->addOrder('creationtime', 'ASC'); - $this->_ci->JobsQueueModel->addLimit('1'); + + // If the maximum amount of jobs to retrieve have been specified + if (is_numeric($maxAmount)) $this->_ci->JobsQueueModel->addLimit($maxAmount); return $this->_ci->JobsQueueModel->loadWhere(array('status' => self::STATUS_NEW, 'type' => $type)); } @@ -221,6 +224,23 @@ class JobsQueueLib return success($results); // otherwise return results in a success object } + //------------------------------------------------------------------------------------------------------------------ + // Public static methods + + /** + * Utility method to generate a job with the given parameters and return it inside an array + * ready to be used by addNewJobsToQueue and updateJobsQueue + */ + public static function generateJobs($status, $input) + { + $job = new stdClass(); + + $job->{self::PROPERTY_STATUS} = $status; + $job->{self::PROPERTY_INPUT} = $input; + + return array($job); + } + //------------------------------------------------------------------------------------------------------------------ // Private methods