Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-10650][BUGFIX] Prevent running again already running cron job #11465

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class ProcessCronQueueObserver implements ObserverInterface
*/
protected $_pendingSchedules;

/**
* @var \Magento\Cron\Model\ResourceModel\Schedule\Collection
*/
private $runningSchedules;

/**
* @var \Magento\Cron\Model\ConfigInterface
*/
Expand Down Expand Up @@ -214,7 +219,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
/** @var \Magento\Cron\Model\Schedule $schedule */
foreach ($pendingJobs as $schedule) {
$jobConfig = isset($jobsRoot[$schedule->getJobCode()]) ? $jobsRoot[$schedule->getJobCode()] : null;
if (!$jobConfig) {
if (!$jobConfig || $this->isJobRunning($schedule->getJobCode())) {
continue;
}

Expand Down Expand Up @@ -250,6 +255,25 @@ public function execute(\Magento\Framework\Event\Observer $observer)
}
}

/**
* @param $jobCode
*
* @return bool
*/
private function isJobRunning($jobCode)
{
$runningJobs = $this->getRunningSchedules();

/** @var \Magento\Cron\Model\Schedule $schedule */
foreach ($runningJobs as $schedule) {
if ($schedule->getData('job_code') == $jobCode) {
return true;
}
}

return false;
}

/**
* Execute job by calling specific class::method
*
Expand Down Expand Up @@ -317,6 +341,22 @@ protected function _getPendingSchedules()
return $this->_pendingSchedules;
}

/**
* Return job collection from data base with status 'running'
*
* @return \Magento\Cron\Model\ResourceModel\Schedule\Collection
*/
private function getRunningSchedules()
{
if (!$this->runningSchedules) {
$this->runningSchedules = $this->_scheduleFactory->create()->getCollection()->addFieldToFilter(
'status',
Schedule::STATUS_RUNNING
)->load();
}
return $this->runningSchedules;
}

/**
* Generate cron schedule
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public function testDispatchCanNotLock()
)->disableOriginalConstructor()->getMock();
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
$scheduleMock->expects($this->any())->method('getResource')->will($this->returnValue($this->scheduleResource));
$this->_scheduleFactory->expects($this->exactly(2))->method('create')->will($this->returnValue($scheduleMock));
$this->_scheduleFactory->expects($this->exactly(3))->method('create')->will($this->returnValue($scheduleMock));

$this->_observer->execute($this->observer);
}
Expand Down Expand Up @@ -333,7 +333,7 @@ public function testDispatchExceptionTooLate()
->disableOriginalConstructor()->getMock();
$scheduleMock->expects($this->any())->method('getCollection')->willReturn($this->_collection);
$scheduleMock->expects($this->any())->method('getResource')->will($this->returnValue($this->scheduleResource));
$this->_scheduleFactory->expects($this->exactly(2))->method('create')->willReturn($scheduleMock);
$this->_scheduleFactory->expects($this->exactly(3))->method('create')->willReturn($scheduleMock);

$this->_observer->execute($this->observer);
}
Expand Down Expand Up @@ -388,7 +388,7 @@ public function testDispatchExceptionNoCallback()
)->disableOriginalConstructor()->getMock();
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
$scheduleMock->expects($this->any())->method('getResource')->will($this->returnValue($this->scheduleResource));
$this->_scheduleFactory->expects($this->exactly(2))->method('create')->will($this->returnValue($scheduleMock));
$this->_scheduleFactory->expects($this->exactly(3))->method('create')->will($this->returnValue($scheduleMock));

$this->_observer->execute($this->observer);
}
Expand Down Expand Up @@ -453,7 +453,7 @@ public function testDispatchExceptionInCallback(
)->disableOriginalConstructor()->getMock();
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
$scheduleMock->expects($this->any())->method('getResource')->will($this->returnValue($this->scheduleResource));
$this->_scheduleFactory->expects($this->exactly(2))->method('create')->will($this->returnValue($scheduleMock));
$this->_scheduleFactory->expects($this->exactly(3))->method('create')->will($this->returnValue($scheduleMock));
$this->_objectManager
->expects($this->once())
->method('create')
Expand Down Expand Up @@ -524,7 +524,7 @@ public function testDispatchRunJob()

// cron end execute some job
$schedule->expects(
$this->at(6)
$this->at(7)
)->method(
'setStatus'
)->with(
Expand All @@ -550,7 +550,7 @@ public function testDispatchRunJob()
)->disableOriginalConstructor()->getMock();
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
$scheduleMock->expects($this->any())->method('getResource')->will($this->returnValue($this->scheduleResource));
$this->_scheduleFactory->expects($this->exactly(2))->method('create')->will($this->returnValue($scheduleMock));
$this->_scheduleFactory->expects($this->exactly(3))->method('create')->will($this->returnValue($scheduleMock));

$testCronJob = $this->getMockBuilder('CronJob')->setMethods(['execute'])->getMock();
$testCronJob->expects($this->atLeastOnce())->method('execute')->with($schedule);
Expand Down