/** * Writes the Job object to the database. If an ID is provided this method will try to update a data set. If there * is no ID given, the system will create a new data set. * * @param Enlight_Components_Cron_Job $job * @return Enlight_Components_Cron_Adapter_Adapter */ public function write(Enlight_Components_Cron_Job $job) { $data = array(); $where = null; foreach ($this->_columns as $key => $value) { switch ($key) { case 'data': $data[$key] = serialize($job->{$key}); break; default: $data[$key] = $job->{$key}; } } if (is_null($job->getId())) { $next = $job->getNext(); if (empty($next)) { $job->setNext(new Zend_Date()); } $start = $job->getStart(); if (empty($start)) { $zd = new Zend_Date(); $job->setStart($zd->subDay(1)); } $end = $job->getEnd(); if (empty($end)) { $zd = new Zend_Date(); $job->setEnd($zd->subDay(1)); } $this->insert($data); } else { $where = $this->getAdapter()->quoteInto($this->getAdapter()->quoteIdentifier($this->_primary) . ' = ?', $job->getId()); $this->update($data, $where); } return $this; }
/** * Ends a job by handing it over to * * @param Enlight_Components_Cron_Job $job * @return void */ protected function endJob(Enlight_Components_Cron_Job $job) { $now = new Zend_Date(); $now = $now->getTimestamp(); $interval = $job->getInterval(); $next = $job->getNext()->getTimestamp(); do { $next += $interval; } while ($now >= $next); $job->setNext($next); $job->setEnd($now); $this->adapter->updateJob($job); }
/** * Starts a job by handing it over to * * @param Enlight_Components_Cron_Job $job * @return void|bool */ private function startJob(Enlight_Components_Cron_Job $job) { $nextRun = $job->getNext(); // get next Date // Turn clock forward do { $nextRun->addSecond($job->getInterval()); } while ($nextRun->compare(new Zend_Date()) >= 0); $job->setStart(new Zend_Date()); $job->setEnd(null); try { $this->_adapter->updateJob($job); $job->setNext($nextRun); return true; } catch (Exception $e) { return false; } }