Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields
Schedule Class Reference
Inheritance diagram for Schedule:
AbstractModel DataObject

Public Member Functions

 __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)
 
 _construct ()
 
 setCronExpr ($expr)
 
 trySchedule ()
 
 matchCronExpression ($expr, $num)
 
 getNumeric ($value)
 
 tryLockJob ()
 
- Public Member Functions inherited from AbstractModel
 __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=[])
 
 __sleep ()
 
 __wakeup ()
 
 setIdFieldName ($name)
 
 getIdFieldName ()
 
 getId ()
 
 setId ($value)
 
 isDeleted ($isDeleted=null)
 
 hasDataChanges ()
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 setDataChanges ($value)
 
 getOrigData ($key=null)
 
 setOrigData ($key=null, $data=null)
 
 dataHasChangedFor ($field)
 
 getResourceName ()
 
 getResourceCollection ()
 
 getCollection ()
 
 load ($modelId, $field=null)
 
 beforeLoad ($identifier, $field=null)
 
 afterLoad ()
 
 isSaveAllowed ()
 
 setHasDataChanges ($flag)
 
 save ()
 
 afterCommitCallback ()
 
 isObjectNew ($flag=null)
 
 beforeSave ()
 
 validateBeforeSave ()
 
 getCacheTags ()
 
 cleanModelCache ()
 
 afterSave ()
 
 delete ()
 
 beforeDelete ()
 
 afterDelete ()
 
 afterDeleteCommit ()
 
 getResource ()
 
 getEntityId ()
 
 setEntityId ($entityId)
 
 clearInstance ()
 
 getStoredData ()
 
 getEventPrefix ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Data Fields

const STATUS_PENDING = 'pending'
 
const STATUS_RUNNING = 'running'
 
const STATUS_SUCCESS = 'success'
 
const STATUS_MISSED = 'missed'
 
const STATUS_ERROR = 'error'
 

Additional Inherited Members

- Protected Member Functions inherited from AbstractModel
 _construct ()
 
 _init ($resourceModel)
 
 _setResourceModel ($resourceName, $collectionName=null)
 
 _getResource ()
 
 _getEventData ()
 
 _beforeLoad ($modelId, $field=null)
 
 _afterLoad ()
 
 _hasModelChanged ()
 
 _getValidatorBeforeSave ()
 
 _createValidatorBeforeSave ()
 
 _getValidationRulesBeforeSave ()
 
 _clearReferences ()
 
 _clearData ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 
- Protected Attributes inherited from AbstractModel
 $_eventPrefix = 'core_abstract'
 
 $_eventObject = 'object'
 
 $_idFieldName = 'id'
 
 $_hasDataChanges = false
 
 $_origData
 
 $_isDeleted = false
 
 $_resource
 
 $_resourceCollection
 
 $_resourceName
 
 $_collectionName
 
 $_cacheTag = false
 
 $_dataSaveAllowed = true
 
 $_isObjectNew = null
 
 $_validatorBeforeSave = null
 
 $_eventManager
 
 $_cacheManager
 
 $_registry
 
 $_logger
 
 $_appState
 
 $_actionValidator
 
 $storedData = []
 
- Protected Attributes inherited from DataObject
 $_data = []
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Crontab schedule model

@method string getJobCode() @method \Magento\Cron\Model\Schedule setJobCode(string $value) @method string getStatus() @method \Magento\Cron\Model\Schedule setStatus(string $value) @method string getMessages() @method \Magento\Cron\Model\Schedule setMessages(string $value) @method string getCreatedAt() @method \Magento\Cron\Model\Schedule setCreatedAt(string $value) @method string getScheduledAt() @method \Magento\Cron\Model\Schedule setScheduledAt(string $value) @method string getExecutedAt() @method \Magento\Cron\Model\Schedule setExecutedAt(string $value) @method string getFinishedAt() @method \Magento\Cron\Model\Schedule setFinishedAt(string $value) @method array getCronExprArr() @method \Magento\Cron\Model\Schedule setCronExprArr(array $value)

@api

Since
100.0.2

Definition at line 36 of file Schedule.php.

Constructor & Destructor Documentation

◆ __construct()

__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 
)
Parameters
\Magento\Framework\Model\Context$context
\Magento\Framework\Registry$registry
\Magento\Framework\Model\ResourceModel\AbstractResource$resource
\Magento\Framework\Data\Collection\AbstractDb$resourceCollection
array$data
TimezoneInterface$timezoneConverter

Definition at line 61 of file Schedule.php.

68  {
69  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
70  $this->timezoneConverter = $timezoneConverter ?: ObjectManager::getInstance()->get(TimezoneInterface::class);
71  }
$resource
Definition: bulk.php:12

Member Function Documentation

◆ _construct()

_construct ( )

Definition at line 76 of file Schedule.php.

77  {
78  $this->_init(\Magento\Cron\Model\ResourceModel\Schedule::class);
79  }

◆ getNumeric()

getNumeric (   $value)

Get number of a month.

Parameters
int | string$value
Returns
bool|int|string

Definition at line 203 of file Schedule.php.

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  }
$value
Definition: gender.phtml:16

◆ matchCronExpression()

matchCronExpression (   $expr,
  $num 
)

Match cron expression.

Parameters
string$expr
int$num
Returns
bool
Exceptions

Definition at line 139 of file Schedule.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
matchCronExpression($expr, $num)
Definition: Schedule.php:139

◆ setCronExpr()

setCronExpr (   $expr)

Set cron expression.

Parameters
string$expr
Returns
$this
Exceptions

Definition at line 88 of file Schedule.php.

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  }
__()
Definition: __.php:13

◆ tryLockJob()

tryLockJob ( )

Lock the cron job so no other scheduled instances run simultaneously.

Sets a job to STATUS_RUNNING only if it is currently in STATUS_PENDING and no other jobs of the same code are currently in STATUS_RUNNING. Returns true if status was changed and false otherwise.

Returns
boolean

Definition at line 250 of file Schedule.php.

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  }

◆ trySchedule()

trySchedule ( )

Checks the observer's cron expression against time.

Supports $this->setCronExpr('* 0-5,10-59/5 2-10,15-25 january-june/2 mon-fri')

Returns
bool

Definition at line 106 of file Schedule.php.

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  }
matchCronExpression($expr, $num)
Definition: Schedule.php:139

Field Documentation

◆ STATUS_ERROR

const STATUS_ERROR = 'error'

Definition at line 46 of file Schedule.php.

◆ STATUS_MISSED

const STATUS_MISSED = 'missed'

Definition at line 44 of file Schedule.php.

◆ STATUS_PENDING

const STATUS_PENDING = 'pending'

Definition at line 38 of file Schedule.php.

◆ STATUS_RUNNING

const STATUS_RUNNING = 'running'

Definition at line 40 of file Schedule.php.

◆ STATUS_SUCCESS

const STATUS_SUCCESS = 'success'

Definition at line 42 of file Schedule.php.


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