Skip to content

Commit bb6356e

Browse files
author
Eugene Tulika
committed
MAGETWO-34526 Process GitHub PR#1052
1 parent f460e84 commit bb6356e

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

app/code/Magento/Cron/Model/Observer.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ protected function _cleanup($groupId)
324324
'system/cron/' . $groupId . '/' . self::XML_PATH_HISTORY_CLEANUP_EVERY,
325325
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
326326
);
327+
$scheduleLifetime = (int)$this->_scopeConfig->getValue(
328+
'system/cron/' . $groupId . '/' . self::XML_PATH_SCHEDULE_LIFETIME,
329+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
330+
);
331+
$scheduleLifetime = $scheduleLifetime * self::SECONDS_IN_MINUTE;
332+
327333
if ($lastCleanup > time() - $historyCleanUp * self::SECONDS_IN_MINUTE) {
328334
return $this;
329335
}
@@ -353,7 +359,9 @@ protected function _cleanup($groupId)
353359
$now = time();
354360
/** @var Schedule $record */
355361
foreach ($history as $record) {
356-
$checkTime = strtotime($record->getExecutedAt() ? $record->getExecutedAt() : $record->getScheduledAt());
362+
$checkTime = strtotime($record->getExecutedAt() ? $record->getExecutedAt() :
363+
(int)$record->getScheduledAt() + $scheduleLifetime
364+
);
357365
if ($checkTime < $now - $historyLifetimes[$record->getStatus()]) {
358366
$record->delete();
359367
}

dev/tests/unit/testsuite/Magento/Cron/Model/ObserverTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,49 @@ public function testDispatchCleanup()
597597

598598
$this->_observer->dispatch('');
599599
}
600+
601+
public function testMissedJobsCleanedInTime()
602+
{
603+
$jobConfig = [
604+
'test_group' => ['test_job1' => ['instance' => 'CronJob', 'method' => 'execute']],
605+
];
606+
607+
$schedule = $this->getMockBuilder(
608+
'Magento\Cron\Model\Schedule'
609+
)->disableOriginalConstructor()->setMethods(
610+
['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup']
611+
)->getMock();
612+
$schedule->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null));
613+
$schedule->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-1 day'));
614+
$schedule->expects($this->any())->method('getStatus')->will($this->returnValue(Schedule::STATUS_MISSED));
615+
616+
$this->_collection->addItem($schedule);
617+
618+
$this->_config->expects($this->once())->method('getJobs')->will($this->returnValue($jobConfig));
619+
620+
$this->_cache->expects($this->at(0))->method('load')->will($this->returnValue(time() + 10000000));
621+
$this->_cache->expects($this->at(1))->method('load')->will($this->returnValue(time() - 10000000));
622+
623+
$this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
624+
625+
$scheduleMock = $this->getMockBuilder('Magento\Cron\Model\Schedule')->disableOriginalConstructor()->getMock();
626+
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
627+
$this->_scheduleFactory->expects($this->at(0))->method('create')->will($this->returnValue($scheduleMock));
628+
629+
$collection = $this->getMockBuilder(
630+
'Magento\Cron\Model\Resource\Schedule\Collection'
631+
)->setMethods(
632+
['addFieldToFilter', 'load', '__wakeup']
633+
)->disableOriginalConstructor()->getMock();
634+
$collection->expects($this->any())->method('addFieldToFilter')->will($this->returnSelf());
635+
$collection->expects($this->any())->method('load')->will($this->returnSelf());
636+
$collection->addItem($schedule);
637+
638+
$scheduleMock = $this->getMockBuilder('Magento\Cron\Model\Schedule')->disableOriginalConstructor()->getMock();
639+
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($collection));
640+
$this->_scheduleFactory->expects($this->at(1))->method('create')->will($this->returnValue($scheduleMock));
641+
642+
$this->_observer->dispatch('');
643+
644+
}
600645
}

0 commit comments

Comments
 (0)