Schedule.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cron\Model\ResourceModel;
  7. /**
  8. * Schedule resource
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Schedule extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * Initialize resource
  17. *
  18. * @return void
  19. */
  20. public function _construct()
  21. {
  22. $this->_init('cron_schedule', 'schedule_id');
  23. }
  24. /**
  25. * Sets new schedule status only if it's in the expected current status.
  26. *
  27. * If schedule is currently in $currentStatus, set it to $newStatus and
  28. * return true. Otherwise, return false.
  29. *
  30. * @param string $scheduleId
  31. * @param string $newStatus
  32. * @param string $currentStatus
  33. * @return bool
  34. */
  35. public function trySetJobStatusAtomic($scheduleId, $newStatus, $currentStatus)
  36. {
  37. $connection = $this->getConnection();
  38. $result = $connection->update(
  39. $this->getTable('cron_schedule'),
  40. ['status' => $newStatus],
  41. ['schedule_id = ?' => $scheduleId, 'status = ?' => $currentStatus]
  42. );
  43. if ($result == 1) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. /**
  49. * Sets schedule status only if no existing schedules with the same job code
  50. * have that status. This is used to implement locking for cron jobs.
  51. *
  52. * If the schedule is currently in $currentStatus and there are no existing
  53. * schedules with the same job code and $newStatus, set the schedule to
  54. * $newStatus and return true. Otherwise, return false.
  55. *
  56. * @param string $scheduleId
  57. * @param string $newStatus
  58. * @param string $currentStatus
  59. * @return bool
  60. * @since 100.2.0
  61. */
  62. public function trySetJobUniqueStatusAtomic($scheduleId, $newStatus, $currentStatus)
  63. {
  64. $connection = $this->getConnection();
  65. // this condition added to avoid cron jobs locking after incorrect termination of running job
  66. $match = $connection->quoteInto(
  67. 'existing.job_code = current.job_code ' .
  68. 'AND (existing.executed_at > UTC_TIMESTAMP() - INTERVAL 1 DAY OR existing.executed_at IS NULL) ' .
  69. 'AND existing.status = ?',
  70. $newStatus
  71. );
  72. $selectIfUnlocked = $connection->select()
  73. ->joinLeft(
  74. ['existing' => $this->getTable('cron_schedule')],
  75. $match,
  76. ['status' => new \Zend_Db_Expr($connection->quote($newStatus))]
  77. )
  78. ->where('current.schedule_id = ?', $scheduleId)
  79. ->where('current.status = ?', $currentStatus)
  80. ->where('existing.schedule_id IS NULL');
  81. $update = $connection->updateFromSelect($selectIfUnlocked, ['current' => $this->getTable('cron_schedule')]);
  82. $result = $connection->query($update)->rowCount();
  83. if ($result == 1) {
  84. return true;
  85. }
  86. return false;
  87. }
  88. }