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 
15 {
21  public function _construct()
22  {
23  $this->_init('cron_schedule', 'schedule_id');
24  }
25 
37  public function trySetJobStatusAtomic($scheduleId, $newStatus, $currentStatus)
38  {
39  $connection = $this->getConnection();
40  $result = $connection->update(
41  $this->getTable('cron_schedule'),
42  ['status' => $newStatus],
43  ['schedule_id = ?' => $scheduleId, 'status = ?' => $currentStatus]
44  );
45  if ($result == 1) {
46  return true;
47  }
48  return false;
49  }
50 
65  public function trySetJobUniqueStatusAtomic($scheduleId, $newStatus, $currentStatus)
66  {
67  $connection = $this->getConnection();
68 
69  // this condition added to avoid cron jobs locking after incorrect termination of running job
70  $match = $connection->quoteInto(
71  'existing.job_code = current.job_code ' .
72  'AND (existing.executed_at > UTC_TIMESTAMP() - INTERVAL 1 DAY OR existing.executed_at IS NULL) ' .
73  'AND existing.status = ?',
74  $newStatus
75  );
76 
77  $selectIfUnlocked = $connection->select()
78  ->joinLeft(
79  ['existing' => $this->getTable('cron_schedule')],
80  $match,
81  ['status' => new \Zend_Db_Expr($connection->quote($newStatus))]
82  )
83  ->where('current.schedule_id = ?', $scheduleId)
84  ->where('current.status = ?', $currentStatus)
85  ->where('existing.schedule_id IS NULL');
86 
87  $update = $connection->updateFromSelect($selectIfUnlocked, ['current' => $this->getTable('cron_schedule')]);
88  $result = $connection->query($update)->rowCount();
89 
90  if ($result == 1) {
91  return true;
92  }
93  return false;
94  }
95 }
trySetJobStatusAtomic($scheduleId, $newStatus, $currentStatus)
Definition: Schedule.php:37
$connection
Definition: bulk.php:13
trySetJobUniqueStatusAtomic($scheduleId, $newStatus, $currentStatus)
Definition: Schedule.php:65