Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Schedule.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Cron\Model;
8 
12 
37 {
38  const STATUS_PENDING = 'pending';
39 
40  const STATUS_RUNNING = 'running';
41 
42  const STATUS_SUCCESS = 'success';
43 
44  const STATUS_MISSED = 'missed';
45 
46  const STATUS_ERROR = 'error';
47 
51  private $timezoneConverter;
52 
61  public function __construct(
62  \Magento\Framework\Model\Context $context,
63  \Magento\Framework\Registry $registry,
64  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
65  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
66  array $data = [],
67  TimezoneInterface $timezoneConverter = null
68  ) {
69  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
70  $this->timezoneConverter = $timezoneConverter ?: ObjectManager::getInstance()->get(TimezoneInterface::class);
71  }
72 
76  public function _construct()
77  {
78  $this->_init(\Magento\Cron\Model\ResourceModel\Schedule::class);
79  }
80 
88  public function setCronExpr($expr)
89  {
90  $e = preg_split('#\s+#', $expr, null, PREG_SPLIT_NO_EMPTY);
91  if (sizeof($e) < 5 || sizeof($e) > 6) {
92  throw new CronException(__('Invalid cron expression: %1', $expr));
93  }
94 
95  $this->setCronExprArr($e);
96  return $this;
97  }
98 
106  public function trySchedule()
107  {
108  $time = $this->getScheduledAt();
109  $e = $this->getCronExprArr();
110 
111  if (!$e || !$time) {
112  return false;
113  }
114  if (!is_numeric($time)) {
115  //convert time from UTC to admin store timezone
116  //we assume that all schedules in configuration (crontab.xml and DB tables) are in admin store timezone
117  $time = $this->timezoneConverter->date($time)->format('Y-m-d H:i');
118  $time = strtotime($time);
119  }
120  $match = $this->matchCronExpression($e[0], strftime('%M', $time))
121  && $this->matchCronExpression($e[1], strftime('%H', $time))
122  && $this->matchCronExpression($e[2], strftime('%d', $time))
123  && $this->matchCronExpression($e[3], strftime('%m', $time))
124  && $this->matchCronExpression($e[4], strftime('%w', $time));
125 
126  return $match;
127  }
128 
139  public function matchCronExpression($expr, $num)
140  {
141  // handle ALL match
142  if ($expr === '*') {
143  return true;
144  }
145 
146  // handle multiple options
147  if (strpos($expr, ',') !== false) {
148  foreach (explode(',', $expr) as $e) {
149  if ($this->matchCronExpression($e, $num)) {
150  return true;
151  }
152  }
153  return false;
154  }
155 
156  // handle modulus
157  if (strpos($expr, '/') !== false) {
158  $e = explode('/', $expr);
159  if (sizeof($e) !== 2) {
160  throw new CronException(__('Invalid cron expression, expecting \'match/modulus\': %1', $expr));
161  }
162  if (!is_numeric($e[1])) {
163  throw new CronException(__('Invalid cron expression, expecting numeric modulus: %1', $expr));
164  }
165  $expr = $e[0];
166  $mod = $e[1];
167  } else {
168  $mod = 1;
169  }
170 
171  // handle all match by modulus
172  if ($expr === '*') {
173  $from = 0;
174  $to = 60;
175  } elseif (strpos($expr, '-') !== false) {
176  // handle range
177  $e = explode('-', $expr);
178  if (sizeof($e) !== 2) {
179  throw new CronException(__('Invalid cron expression, expecting \'from-to\' structure: %1', $expr));
180  }
181 
182  $from = $this->getNumeric($e[0]);
183  $to = $this->getNumeric($e[1]);
184  } else {
185  // handle regular token
186  $from = $this->getNumeric($expr);
187  $to = $from;
188  }
189 
190  if ($from === false || $to === false) {
191  throw new CronException(__('Invalid cron expression: %1', $expr));
192  }
193 
194  return $num >= $from && $num <= $to && $num % $mod === 0;
195  }
196 
203  public function getNumeric($value)
204  {
205  static $data = [
206  'jan' => 1,
207  'feb' => 2,
208  'mar' => 3,
209  'apr' => 4,
210  'may' => 5,
211  'jun' => 6,
212  'jul' => 7,
213  'aug' => 8,
214  'sep' => 9,
215  'oct' => 10,
216  'nov' => 11,
217  'dec' => 12,
218  'sun' => 0,
219  'mon' => 1,
220  'tue' => 2,
221  'wed' => 3,
222  'thu' => 4,
223  'fri' => 5,
224  'sat' => 6,
225  ];
226 
227  if (is_numeric($value)) {
228  return $value;
229  }
230 
231  if (is_string($value)) {
232  $value = strtolower(substr($value, 0, 3));
233  if (isset($data[$value])) {
234  return $data[$value];
235  }
236  }
237 
238  return false;
239  }
240 
250  public function tryLockJob()
251  {
252  if ($this->_getResource()->trySetJobUniqueStatusAtomic(
253  $this->getId(),
254  self::STATUS_RUNNING,
255  self::STATUS_PENDING
256  )) {
257  $this->setStatus(self::STATUS_RUNNING);
258  return true;
259  }
260  return false;
261  }
262 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], TimezoneInterface $timezoneConverter=null)
Definition: Schedule.php:61
matchCronExpression($expr, $num)
Definition: Schedule.php:139