Mage_Cron_Model_Observer Class Reference

List of all members.

Public Member Functions

 dispatch ($observer)
 getPendingSchedules ()
 generate ()
 cleanup ()

Public Attributes

const CACHE_KEY_LAST_SCHEDULE_GENERATE_AT = 'cron_last_schedule_generate_at'
const CACHE_KEY_LAST_HISTORY_CLEANUP_AT = 'cron_last_history_cleanup_at'
const XML_PATH_SCHEDULE_GENERATE_EVERY = 'system/cron/schedule_generate_freq'
const XML_PATH_SCHEDULE_AHEAD_FOR = 'system/cron/schedule_ahead_for'
const XML_PATH_SCHEDULE_LIFETIME = 'system/cron/schedule_lifetime'
const XML_PATH_HISTORY_CLEANUP_EVERY = 'system/cron/history_cleanup_every'
const XML_PATH_HISTORY_SUCCESS = 'system/cron/history_success_lifetime'
const XML_PATH_HISTORY_FAILURE = 'system/cron/history_failure_lifetime'
const REGEX_RUN_MODEL = '#^([a-z0-9_]+/[a-z0-9_]+)::([a-z0-9_]+)$#i'

Protected Member Functions

 _generateJobs ($jobs, $exists)

Protected Attributes

 $_pendingSchedules


Detailed Description

Definition at line 34 of file Observer.php.


Member Function Documentation

_generateJobs ( jobs,
exists 
) [protected]

Generate jobs for config information

Parameters:
$jobs 
array $exists
Returns:
Mage_Cron_Model_Observer

Definition at line 181 of file Observer.php.

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                     // already scheduled
00208                     continue;
00209                 }
00210                 if (!$schedule->trySchedule($time)) {
00211                     // time does not match cron expression
00212                     continue;
00213                 }
00214                 $schedule->unsScheduleId()->save();
00215             }
00216         }
00217         return $this;
00218     }

cleanup (  ) 

Definition at line 220 of file Observer.php.

00221     {
00222         // check if history cleanup is needed
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         // save time history cleanup was ran with no expiration
00249         Mage::app()->saveCache(time(), self::CACHE_KEY_LAST_HISTORY_CLEANUP_AT, array('crontab'), null);
00250 
00251         return $this;
00252     }

dispatch ( observer  ) 

Process cron queue Geterate tasks schedule Cleanup tasks schedule

Parameters:
Varien_Event_Observer $observer

Definition at line 57 of file Observer.php.

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     }

generate (  ) 

Generate cron schedule

Returns:
Mage_Cron_Model_Observer

check if schedule generation is needed

generate global crontab jobs

generate configurable crontab jobs

save time schedules generation was ran with no expiration

Definition at line 134 of file Observer.php.

00135     {
00136         /**
00137          * check if schedule generation is needed
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          * generate global crontab jobs
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          * generate configurable crontab jobs
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          * save time schedules generation was ran with no expiration
00168          */
00169         Mage::app()->saveCache(time(), self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT, array('crontab'), null);
00170 
00171         return $this;
00172     }

getPendingSchedules (  ) 

Definition at line 119 of file Observer.php.

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     }


Member Data Documentation

$_pendingSchedules [protected]

Definition at line 48 of file Observer.php.

const CACHE_KEY_LAST_HISTORY_CLEANUP_AT = 'cron_last_history_cleanup_at'

Definition at line 37 of file Observer.php.

const CACHE_KEY_LAST_SCHEDULE_GENERATE_AT = 'cron_last_schedule_generate_at'

Definition at line 36 of file Observer.php.

const REGEX_RUN_MODEL = '#^([a-z0-9_]+/[a-z0-9_]+)::([a-z0-9_]+)$#i'

Definition at line 46 of file Observer.php.

const XML_PATH_HISTORY_CLEANUP_EVERY = 'system/cron/history_cleanup_every'

Definition at line 42 of file Observer.php.

const XML_PATH_HISTORY_FAILURE = 'system/cron/history_failure_lifetime'

Definition at line 44 of file Observer.php.

const XML_PATH_HISTORY_SUCCESS = 'system/cron/history_success_lifetime'

Definition at line 43 of file Observer.php.

const XML_PATH_SCHEDULE_AHEAD_FOR = 'system/cron/schedule_ahead_for'

Definition at line 40 of file Observer.php.

const XML_PATH_SCHEDULE_GENERATE_EVERY = 'system/cron/schedule_generate_freq'

Definition at line 39 of file Observer.php.

const XML_PATH_SCHEDULE_LIFETIME = 'system/cron/schedule_lifetime'

Definition at line 41 of file Observer.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:01 2009 for Magento by  doxygen 1.5.8