PageRenderTime 20ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Cron/Model/Schedule.php

https://github.com/rgranadino/magento-mirror
PHP | 219 lines | 128 code | 21 blank | 70 comment | 27 complexity | e0c5859c4101c67cfcb86443366bc0f7 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Cron
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Crontab schedule model
  28. *
  29. * @method Mage_Cron_Model_Resource_Schedule _getResource()
  30. * @method Mage_Cron_Model_Resource_Schedule getResource()
  31. * @method string getJobCode()
  32. * @method Mage_Cron_Model_Schedule setJobCode(string $value)
  33. * @method string getStatus()
  34. * @method Mage_Cron_Model_Schedule setStatus(string $value)
  35. * @method string getMessages()
  36. * @method Mage_Cron_Model_Schedule setMessages(string $value)
  37. * @method string getCreatedAt()
  38. * @method Mage_Cron_Model_Schedule setCreatedAt(string $value)
  39. * @method string getScheduledAt()
  40. * @method Mage_Cron_Model_Schedule setScheduledAt(string $value)
  41. * @method string getExecutedAt()
  42. * @method Mage_Cron_Model_Schedule setExecutedAt(string $value)
  43. * @method string getFinishedAt()
  44. * @method Mage_Cron_Model_Schedule setFinishedAt(string $value)
  45. *
  46. * @category Mage
  47. * @package Mage_Cron
  48. * @author Magento Core Team <core@magentocommerce.com>
  49. */
  50. class Mage_Cron_Model_Schedule extends Mage_Core_Model_Abstract
  51. {
  52. const STATUS_PENDING = 'pending';
  53. const STATUS_RUNNING = 'running';
  54. const STATUS_SUCCESS = 'success';
  55. const STATUS_MISSED = 'missed';
  56. const STATUS_ERROR = 'error';
  57. public function _construct()
  58. {
  59. $this->_init('cron/schedule');
  60. }
  61. public function setCronExpr($expr)
  62. {
  63. $e = preg_split('#\s+#', $expr, null, PREG_SPLIT_NO_EMPTY);
  64. if (sizeof($e)<5 || sizeof($e)>6) {
  65. throw Mage::exception('Mage_Cron', 'Invalid cron expression: '.$expr);
  66. }
  67. $this->setCronExprArr($e);
  68. return $this;
  69. }
  70. /**
  71. * Checks the observer's cron expression against time
  72. *
  73. * Supports $this->setCronExpr('* 0-5,10-59/5 2-10,15-25 january-june/2 mon-fri')
  74. *
  75. * @param Varien_Event $event
  76. * @return boolean
  77. */
  78. public function trySchedule($time)
  79. {
  80. $e = $this->getCronExprArr();
  81. if (!$e || !$time) {
  82. return false;
  83. }
  84. if (!is_numeric($time)) {
  85. $time = strtotime($time);
  86. }
  87. $d = getdate(Mage::getSingleton('core/date')->timestamp($time));
  88. $match = $this->matchCronExpression($e[0], $d['minutes'])
  89. && $this->matchCronExpression($e[1], $d['hours'])
  90. && $this->matchCronExpression($e[2], $d['mday'])
  91. && $this->matchCronExpression($e[3], $d['mon'])
  92. && $this->matchCronExpression($e[4], $d['wday']);
  93. if ($match) {
  94. $this->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', time()));
  95. $this->setScheduledAt(strftime('%Y-%m-%d %H:%M', $time));
  96. }
  97. return $match;
  98. }
  99. public function matchCronExpression($expr, $num)
  100. {
  101. // handle ALL match
  102. if ($expr==='*') {
  103. return true;
  104. }
  105. // handle multiple options
  106. if (strpos($expr,',')!==false) {
  107. foreach (explode(',',$expr) as $e) {
  108. if ($this->matchCronExpression($e, $num)) {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. // handle modulus
  115. if (strpos($expr,'/')!==false) {
  116. $e = explode('/', $expr);
  117. if (sizeof($e)!==2) {
  118. throw Mage::exception('Mage_Cron', "Invalid cron expression, expecting 'match/modulus': ".$expr);
  119. }
  120. if (!is_numeric($e[1])) {
  121. throw Mage::exception('Mage_Cron', "Invalid cron expression, expecting numeric modulus: ".$expr);
  122. }
  123. $expr = $e[0];
  124. $mod = $e[1];
  125. } else {
  126. $mod = 1;
  127. }
  128. // handle all match by modulus
  129. if ($expr==='*') {
  130. $from = 0;
  131. $to = 60;
  132. }
  133. // handle range
  134. elseif (strpos($expr,'-')!==false) {
  135. $e = explode('-', $expr);
  136. if (sizeof($e)!==2) {
  137. throw Mage::exception('Mage_Cron', "Invalid cron expression, expecting 'from-to' structure: ".$expr);
  138. }
  139. $from = $this->getNumeric($e[0]);
  140. $to = $this->getNumeric($e[1]);
  141. }
  142. // handle regular token
  143. else {
  144. $from = $this->getNumeric($expr);
  145. $to = $from;
  146. }
  147. if ($from===false || $to===false) {
  148. throw Mage::exception('Mage_Cron', "Invalid cron expression: ".$expr);
  149. }
  150. return ($num>=$from) && ($num<=$to) && ($num%$mod===0);
  151. }
  152. public function getNumeric($value)
  153. {
  154. static $data = array(
  155. 'jan'=>1,
  156. 'feb'=>2,
  157. 'mar'=>3,
  158. 'apr'=>4,
  159. 'may'=>5,
  160. 'jun'=>6,
  161. 'jul'=>7,
  162. 'aug'=>8,
  163. 'sep'=>9,
  164. 'oct'=>10,
  165. 'nov'=>11,
  166. 'dec'=>12,
  167. 'sun'=>0,
  168. 'mon'=>1,
  169. 'tue'=>2,
  170. 'wed'=>3,
  171. 'thu'=>4,
  172. 'fri'=>5,
  173. 'sat'=>6,
  174. );
  175. if (is_numeric($value)) {
  176. return $value;
  177. }
  178. if (is_string($value)) {
  179. $value = strtolower(substr($value,0,3));
  180. if (isset($data[$value])) {
  181. return $data[$value];
  182. }
  183. }
  184. return false;
  185. }
  186. /**
  187. * Sets a job to STATUS_RUNNING only if it is currently in STATUS_PENDING.
  188. * Returns true if status was changed and false otherwise.
  189. *
  190. * This is used to implement locking for cron jobs.
  191. *
  192. * @return boolean
  193. */
  194. public function tryLockJob()
  195. {
  196. return $this->_getResource()->trySetJobStatusAtomic($this->getId(), self::STATUS_RUNNING,self::STATUS_PENDING);
  197. }
  198. }