00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Cron_Model_Observer
00035 {
00036 const CACHE_KEY_LAST_SCHEDULE_GENERATE_AT = 'cron_last_schedule_generate_at';
00037 const CACHE_KEY_LAST_HISTORY_CLEANUP_AT = 'cron_last_history_cleanup_at';
00038
00039 const XML_PATH_SCHEDULE_GENERATE_EVERY = 'system/cron/schedule_generate_freq';
00040 const XML_PATH_SCHEDULE_AHEAD_FOR = 'system/cron/schedule_ahead_for';
00041 const XML_PATH_SCHEDULE_LIFETIME = 'system/cron/schedule_lifetime';
00042 const XML_PATH_HISTORY_CLEANUP_EVERY = 'system/cron/history_cleanup_every';
00043 const XML_PATH_HISTORY_SUCCESS = 'system/cron/history_success_lifetime';
00044 const XML_PATH_HISTORY_FAILURE = 'system/cron/history_failure_lifetime';
00045
00046 const REGEX_RUN_MODEL = '#^([a-z0-9_]+/[a-z0-9_]+)::([a-z0-9_]+)$#i';
00047
00048 protected $_pendingSchedules;
00049
00050
00051
00052
00053
00054
00055
00056
00057 public function dispatch($observer)
00058 {
00059 $schedules = $this->getPendingSchedules();
00060 $scheduleLifetime = Mage::getStoreConfig(self::XML_PATH_SCHEDULE_LIFETIME) * 60;
00061 $now = time();
00062 $jobsRoot = Mage::getConfig()->getNode('crontab/jobs');
00063
00064 foreach ($schedules->getIterator() as $schedule) {
00065 $jobConfig = $jobsRoot->{$schedule->getJobCode()};
00066 if (!$jobConfig || !$jobConfig->run) {
00067 continue;
00068 }
00069
00070 $runConfig = $jobConfig->run;
00071 $time = strtotime($schedule->getScheduledAt());
00072 if ($time > $now) {
00073 continue;
00074 }
00075 try {
00076 $errorStatus = Mage_Cron_Model_Schedule::STATUS_ERROR;
00077 $errorMessage = Mage::helper('cron')->__('Unknown error');
00078
00079 if ($time < $now - $scheduleLifetime) {
00080 $errorStatus = Mage_Cron_Model_Schedule::STATUS_MISSED;
00081 Mage::throwException(Mage::helper('cron')->__('Too late for the schedule'));
00082 }
00083
00084 $schedule->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', time()));
00085
00086 if ($runConfig->model) {
00087 if (!preg_match(self::REGEX_RUN_MODEL, (string)$runConfig->model, $run)) {
00088 Mage::throwException(Mage::helper('cron')->__('Invalid model/method definition, expecting "model/class::method".'));
00089 }
00090 if (!($model = Mage::getModel($run[1])) || !method_exists($model, $run[2])) {
00091 Mage::throwException(Mage::helper('cron')->__('Invalid callback: %s::%s does not exist', $run[1], $run[2]));
00092 }
00093 $callback = array($model, $run[2]);
00094 $arguments = array($schedule);
00095 }
00096 if (empty($callback)) {
00097 Mage::throwException(Mage::helper('cron')->__('No callbacks found'));
00098 }
00099
00100 $schedule->setStatus(Mage_Cron_Model_Schedule::STATUS_RUNNING)
00101 ->save();
00102
00103 call_user_func_array($callback, $arguments);
00104
00105 $schedule->setStatus(Mage_Cron_Model_Schedule::STATUS_SUCCESS)
00106 ->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', time()));
00107
00108 } catch (Exception $e) {
00109 $schedule->setStatus($errorStatus)
00110 ->setMessages($e->getMessage());
00111 }
00112 $schedule->save();
00113 }
00114
00115 $this->generate();
00116 $this->cleanup();
00117 }
00118
00119 public function getPendingSchedules()
00120 {
00121 if (!$this->_pendingSchedules) {
00122 $this->_pendingSchedules = Mage::getModel('cron/schedule')->getCollection()
00123 ->addFieldToFilter('status', Mage_Cron_Model_Schedule::STATUS_PENDING)
00124 ->load();
00125 }
00126 return $this->_pendingSchedules;
00127 }
00128
00129
00130
00131
00132
00133
00134 public function generate()
00135 {
00136
00137
00138
00139 $lastRun = Mage::app()->loadCache(self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT);
00140 if ($lastRun > time() - Mage::getStoreConfig(self::XML_PATH_SCHEDULE_GENERATE_EVERY)*60) {
00141 return $this;
00142 }
00143
00144 $schedules = $this->getPendingSchedules();
00145 $exists = array();
00146 foreach ($schedules->getIterator() as $schedule) {
00147 $exists[$schedule->getJobCode().'/'.$schedule->getScheduledAt()] = 1;
00148 }
00149
00150
00151
00152
00153 $config = Mage::getConfig()->getNode('crontab/jobs');
00154 if ($config instanceof Mage_Core_Model_Config_Element) {
00155 $this->_generateJobs($config->children(), $exists);
00156 }
00157
00158
00159
00160
00161 $config = Mage::getConfig()->getNode('default/crontab/jobs');
00162 if ($config instanceof Mage_Core_Model_Config_Element) {
00163 $this->_generateJobs($config->children(), $exists);
00164 }
00165
00166
00167
00168
00169 Mage::app()->saveCache(time(), self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT, array('crontab'), null);
00170
00171 return $this;
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181 protected function _generateJobs($jobs, $exists)
00182 {
00183 $scheduleAheadFor = Mage::getStoreConfig(self::XML_PATH_SCHEDULE_AHEAD_FOR)*60;
00184 $schedule = Mage::getModel('cron/schedule');
00185
00186 foreach ($jobs as $jobCode => $jobConfig) {
00187 $cronExpr = null;
00188 if ($jobConfig->schedule->config_path) {
00189 $cronExpr = Mage::getStoreConfig((string)$jobConfig->schedule->config_path);
00190 }
00191 if (empty($cronExpr) && $jobConfig->schedule->cron_expr) {
00192 $cronExpr = (string)$jobConfig->schedule->cron_expr;
00193 }
00194 if (!$cronExpr) {
00195 continue;
00196 }
00197
00198 $now = time();
00199 $timeAhead = $now + $scheduleAheadFor;
00200 $schedule->setJobCode($jobCode)
00201 ->setCronExpr($cronExpr)
00202 ->setStatus(Mage_Cron_Model_Schedule::STATUS_PENDING);
00203
00204 for ($time = $now; $time < $timeAhead; $time += 60) {
00205 $ts = strftime('%Y-%m-%d %H:%M:00', $time);
00206 if (!empty($exists[$jobCode.'/'.$ts])) {
00207
00208 continue;
00209 }
00210 if (!$schedule->trySchedule($time)) {
00211
00212 continue;
00213 }
00214 $schedule->unsScheduleId()->save();
00215 }
00216 }
00217 return $this;
00218 }
00219
00220 public function cleanup()
00221 {
00222
00223 $lastCleanup = Mage::app()->loadCache(self::CACHE_KEY_LAST_HISTORY_CLEANUP_AT);
00224 if ($lastCleanup > time() - Mage::getStoreConfig(self::XML_PATH_HISTORY_CLEANUP_EVERY)*60) {
00225 return $this;
00226 }
00227
00228 $history = Mage::getModel('cron/schedule')->getCollection()
00229 ->addFieldToFilter('status', array('in'=>array(
00230 Mage_Cron_Model_Schedule::STATUS_SUCCESS,
00231 Mage_Cron_Model_Schedule::STATUS_MISSED,
00232 Mage_Cron_Model_Schedule::STATUS_ERROR,
00233 )))->load();
00234
00235 $historyLifetimes = array(
00236 Mage_Cron_Model_Schedule::STATUS_SUCCESS => Mage::getStoreConfig(self::XML_PATH_HISTORY_SUCCESS)*60,
00237 Mage_Cron_Model_Schedule::STATUS_MISSED => Mage::getStoreConfig(self::XML_PATH_HISTORY_FAILURE)*60,
00238 Mage_Cron_Model_Schedule::STATUS_ERROR => Mage::getStoreConfig(self::XML_PATH_HISTORY_FAILURE)*60,
00239 );
00240
00241 $now = time();
00242 foreach ($history->getIterator() as $record) {
00243 if (strtotime($record->getExecutedAt()) < $now-$historyLifetimes[$record->getStatus()]) {
00244 $record->delete();
00245 }
00246 }
00247
00248
00249 Mage::app()->saveCache(time(), self::CACHE_KEY_LAST_HISTORY_CLEANUP_AT, array('crontab'), null);
00250
00251 return $this;
00252 }
00253 }