diff --git a/app/code/Magento/Cron/Model/ResourceModel/Schedule.php b/app/code/Magento/Cron/Model/ResourceModel/Schedule.php index 25dd02c207f4e..4b481c8bc17a5 100644 --- a/app/code/Magento/Cron/Model/ResourceModel/Schedule.php +++ b/app/code/Magento/Cron/Model/ResourceModel/Schedule.php @@ -1,6 +1,6 @@ getConnection(); // this condition added to avoid cron jobs locking after incorrect termination of running job - $match = $connection->quoteInto( - 'existing.job_code = current.job_code ' . - 'AND (existing.executed_at > UTC_TIMESTAMP() - INTERVAL 1 DAY OR existing.executed_at IS NULL) ' . - 'AND existing.status = ?', - $newStatus - ); + + $match = $connection->select() + ->from(['schedule' => $this->getTable('cron_schedule')]) + ->where('schedule.schedule_id = ?', $scheduleId); + + $result = $connection->fetchAll($match); + $jobcode = $result[0]["job_code"]; $selectIfUnlocked = $connection->select() - ->joinLeft( - ['existing' => $this->getTable('cron_schedule')], - $match, - ['status' => new \Zend_Db_Expr($connection->quote($newStatus))] - ) - ->where('current.schedule_id = ?', $scheduleId) - ->where('current.status = ?', $currentStatus) - ->where('existing.schedule_id IS NULL'); + ->from(['schedule' => $this->getTable('cron_schedule')]) + ->where('schedule.job_code = ?', $jobcode) + ->where('schedule.scheduled_at > UTC_TIMESTAMP() - INTERVAL 1 DAY') + ->where('schedule.status = ? ', $newStatus) + ->where('schedule.status != ? ', $currentStatus); - $update = $connection->updateFromSelect($selectIfUnlocked, ['current' => $this->getTable('cron_schedule')]); - $result = $connection->query($update)->rowCount(); + $result = count($connection->fetchAll($selectIfUnlocked)); - if ($result == 1) { - return true; + if ($result == 0) { + $where = $connection->quoteInto('schedule_id =?', $scheduleId); + $result = $connection->update($this->getTable('cron_schedule'), ['status'=>$newStatus], $where); + + if ($result == 1) { + return true; + } } return false; }