public function executeJobs($schedule = null) { if (!Mage::getStoreConfig('jobqueue/config/enabled')) { return; } if ($schedule) { $jobsRoot = Mage::getConfig()->getNode('crontab/jobs'); $jobConfig = $jobsRoot->{$schedule->getJobCode()}; $queue = $jobConfig->queue; if ($queue) { $this->setQueue($queue); } } $this->setupDJJob(); try { $collection = Mage::getModel('jobqueue/job')->getCollection(); $collection->addFieldToFilter('queue', array('eq' => $this->getQueue()))->addFieldToFilter('run_at', array(array('null' => true), array('lteq' => now())))->addFieldToFilter(array('locked_at', 'locked_by'), array(array('locked_at', 'null' => true), array('locked_by', 'eq' => $this->workerName)))->addFieldToFilter('failed_at', array('null' => true))->addFieldToFilter('attempts', array('lt' => (int) Mage::getStoreConfig('jobqueue/config/max_attempts'))); // randomly order to prevent lock contention among workers $collection->getSelect()->order(new Zend_Db_Expr('RAND()')); $collection->load(); foreach ($collection as $row) { $job = new DJJob($this->workerName, $row->getId(), array("max_attempts" => Mage::getStoreConfig('jobqueue/config/max_attempts'))); if ($job->acquireLock()) { $job->run(); } } } catch (Exception $e) { Mage::logException($e); } }
/** * Returns a new job ordered by most recent first * why this? * run newest first, some jobs get left behind * run oldest first, all jobs get left behind * @return DJJob */ public function getNewJob() { # we can grab a locked job if we own the lock $rs = $this->runQuery("\n SELECT id\n FROM " . self::$jobsTable . "\n WHERE queue = ?\n AND (run_at IS NULL OR NOW() >= run_at)\n AND (locked_at IS NULL OR locked_by = ?)\n AND failed_at IS NULL\n AND attempts < ?\n ORDER BY created_at DESC\n LIMIT 10\n ", array($this->queue, $this->name, $this->max_attempts)); // randomly order the 10 to prevent lock contention among workers shuffle($rs); foreach ($rs as $r) { $job = new DJJob($this->name, $r["id"], array("max_attempts" => $this->max_attempts, "fail_on_output" => $this->fail_on_output)); if ($job->acquireLock()) { return $job; } } return false; }