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_Schedule extends Mage_Core_Model_Abstract
00035 {
00036 const STATUS_PENDING = 'pending';
00037 const STATUS_RUNNING = 'running';
00038 const STATUS_SUCCESS = 'success';
00039 const STATUS_MISSED = 'missed';
00040 const STATUS_ERROR = 'error';
00041
00042 public function _construct()
00043 {
00044 $this->_init('cron/schedule');
00045 }
00046
00047 public function setCronExpr($expr)
00048 {
00049 $e = preg_split('#\s+#', $expr, null, PREG_SPLIT_NO_EMPTY);
00050 if (sizeof($e)<5 || sizeof($e)>6) {
00051 throw Mage::exception('Mage_Cron', 'Invalid cron expression: '.$expr);
00052 }
00053
00054 $this->setCronExprArr($e);
00055 return $this;
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 public function trySchedule($time)
00067 {
00068 $e = $this->getCronExprArr();
00069 if (!$e || !$time) {
00070 return false;
00071 }
00072 if (!is_numeric($time)) {
00073 $time = strtotime($time);
00074 }
00075
00076 $d = getdate(Mage::getSingleton('core/date')->timestamp($time));
00077
00078 $match = $this->matchCronExpression($e[0], $d['minutes'])
00079 && $this->matchCronExpression($e[1], $d['hours'])
00080 && $this->matchCronExpression($e[2], $d['mday'])
00081 && $this->matchCronExpression($e[3], $d['mon'])
00082 && $this->matchCronExpression($e[4], $d['wday']);
00083
00084 if ($match) {
00085 $this->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', time()));
00086 $this->setScheduledAt(strftime('%Y-%m-%d %H:%M', $time));
00087 }
00088 return $match;
00089 }
00090
00091 public function matchCronExpression($expr, $num)
00092 {
00093
00094 if ($expr==='*') {
00095 return true;
00096 }
00097
00098
00099 if (strpos($expr,',')!==false) {
00100 foreach (explode(',',$expr) as $e) {
00101 if ($this->matchCronExpression($e, $num)) {
00102 return true;
00103 }
00104 }
00105 return false;
00106 }
00107
00108
00109 if (strpos($expr,'/')!==false) {
00110 $e = explode('/', $expr);
00111 if (sizeof($e)!==2) {
00112 throw Mage::exception('Mage_Cron', "Invalid cron expression, expecting 'match/modulus': ".$expr);
00113 }
00114 if (!is_numeric($e[1])) {
00115 throw Mage::exception('Mage_Cron', "Invalid cron expression, expecting numeric modulus: ".$expr);
00116 }
00117 $expr = $e[0];
00118 $mod = $e[1];
00119 } else {
00120 $mod = 1;
00121 }
00122
00123
00124 if ($expr==='*') {
00125 $from = 0;
00126 $to = 60;
00127 }
00128
00129 elseif (strpos($expr,'-')!==false) {
00130 $e = explode('-', $expr);
00131 if (sizeof($e)!==2) {
00132 throw Mage::exception('Mage_Cron', "Invalid cron expression, expecting 'from-to' structure: ".$expr);
00133 }
00134
00135 $from = $this->getNumeric($e[0]);
00136 $to = $this->getNumeric($e[1]);
00137 }
00138
00139 else {
00140 $from = $this->getNumeric($expr);
00141 $to = $from;
00142 }
00143
00144 if ($from===false || $to===false) {
00145 throw Mage::exception('Mage_Cron', "Invalid cron expression: ".$expr);
00146 }
00147
00148 return ($num>=$from) && ($num<=$to) && ($num%$mod===0);
00149 }
00150
00151 public function getNumeric($value)
00152 {
00153 static $data = array(
00154 'jan'=>1,
00155 'feb'=>2,
00156 'mar'=>3,
00157 'apr'=>4,
00158 'may'=>5,
00159 'jun'=>6,
00160 'jul'=>7,
00161 'aug'=>8,
00162 'sep'=>9,
00163 'oct'=>10,
00164 'nov'=>11,
00165 'dec'=>12,
00166
00167 'sun'=>0,
00168 'mon'=>1,
00169 'tue'=>2,
00170 'wed'=>3,
00171 'thu'=>4,
00172 'fri'=>5,
00173 'sat'=>6,
00174 );
00175
00176 if (is_numeric($value)) {
00177 return $value;
00178 }
00179
00180 if (is_string($value)) {
00181 $value = strtolower(substr($value,0,3));
00182 if (isset($data[$value])) {
00183 return $data[$value];
00184 }
00185 }
00186
00187 return false;
00188 }
00189 }