Skip to content

Commit 43b7ef4

Browse files
author
Eugene Tulika
committed
Merge branch 'MAGETWO-34526' into Accept-Public-PRs
Conflicts: app/code/Magento/Cron/Model/Observer.php
2 parents c0eef7c + 4c7b2a5 commit 43b7ef4

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,13 @@ protected function _cleanup($groupId)
345345
return $this;
346346
}
347347

348+
// check how long the record should stay unprocessed before marked as MISSED
349+
$scheduleLifetime = (int)$this->_scopeConfig->getValue(
350+
'system/cron/' . $groupId . '/' . self::XML_PATH_SCHEDULE_LIFETIME,
351+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
352+
);
353+
$scheduleLifetime = $scheduleLifetime * self::SECONDS_IN_MINUTE;
354+
348355
/**
349356
* @var \Magento\Cron\Model\Resource\Schedule\Collection $history
350357
*/
@@ -370,7 +377,9 @@ protected function _cleanup($groupId)
370377
$now = $this->timezone->scopeTimeStamp();
371378
/** @var Schedule $record */
372379
foreach ($history as $record) {
373-
if (strtotime($record->getExecutedAt()) < $now - $historyLifetimes[$record->getStatus()]) {
380+
$checkTime = $record->getExecutedAt() ? strtotime($record->getExecutedAt()) :
381+
strtotime($record->getScheduledAt()) + $scheduleLifetime;
382+
if ($checkTime < $now - $historyLifetimes[$record->getStatus()]) {
374383
$record->delete();
375384
}
376385
}

app/code/Magento/Cron/Test/Unit/Model/ObserverTest.php

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* See COPYING.txt for license details.
55
*/
66
namespace Magento\Cron\Test\Unit\Model;
7-
7+
use Magento\Cron\Model\Schedule;
88
/**
99
* Class \Magento\Cron\Test\Unit\Model\ObserverTest
1010
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -607,4 +607,87 @@ public function testDispatchCleanup()
607607

608608
$this->_observer->dispatch('');
609609
}
610+
611+
public function testMissedJobsCleanedInTime()
612+
{
613+
/* 1. Initialize dependencies of _generate() method which is called first */
614+
$jobConfig = [
615+
'test_group' => ['test_job1' => ['instance' => 'CronJob', 'method' => 'execute']],
616+
];
617+
618+
// This item was scheduled 2 days ago
619+
$schedule1 = $this->getMockBuilder(
620+
'Magento\Cron\Model\Schedule'
621+
)->disableOriginalConstructor()->setMethods(
622+
['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup']
623+
)->getMock();
624+
$schedule1->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null));
625+
$schedule1->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-2 day -1 hour'));
626+
$schedule1->expects($this->any())->method('getStatus')->will(
627+
$this->returnValue(Schedule::STATUS_MISSED));
628+
//we expect this job be deleted from the list
629+
$schedule1->expects($this->once())->method('delete')->will(
630+
$this->returnValue(true));
631+
632+
// This item was scheduled 1 day ago
633+
$schedule2 = $this->getMockBuilder(
634+
'Magento\Cron\Model\Schedule'
635+
)->disableOriginalConstructor()->setMethods(
636+
['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup']
637+
)->getMock();
638+
$schedule2->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null));
639+
$schedule2->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-1 day'));
640+
$schedule2->expects($this->any())->method('getStatus')->will(
641+
$this->returnValue(Schedule::STATUS_MISSED));
642+
//we don't expect this job be deleted from the list
643+
$schedule2->expects($this->never())->method('delete');
644+
645+
$this->_collection->addItem($schedule1);
646+
$this->_config->expects($this->once())->method('getJobs')->will($this->returnValue($jobConfig));
647+
648+
//get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_generate()"
649+
$this->_cache->expects($this->at(0))->method('load')->will($this->returnValue(time() + 10000000));
650+
//get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_cleanup()"
651+
$this->_cache->expects($this->at(1))->method('load')->will($this->returnValue(time() - 10000000));
652+
653+
$this->_scopeConfig->expects($this->at(0))->method('getValue')
654+
->with($this->equalTo('system/cron/test_group/use_separate_process'))
655+
->will($this->returnValue(0));
656+
$this->_scopeConfig->expects($this->at(1))->method('getValue')
657+
->with($this->equalTo('system/cron/test_group/schedule_generate_every'))
658+
->will($this->returnValue(0));
659+
$this->_scopeConfig->expects($this->at(2))->method('getValue')
660+
->with($this->equalTo('system/cron/test_group/history_cleanup_every'))
661+
->will($this->returnValue(0));
662+
$this->_scopeConfig->expects($this->at(3))->method('getValue')
663+
->with($this->equalTo('system/cron/test_group/schedule_lifetime'))
664+
->will($this->returnValue(2*24*60));
665+
$this->_scopeConfig->expects($this->at(4))->method('getValue')
666+
->with($this->equalTo('system/cron/test_group/history_success_lifetime'))
667+
->will($this->returnValue(0));
668+
$this->_scopeConfig->expects($this->at(5))->method('getValue')
669+
->with($this->equalTo('system/cron/test_group/history_failure_lifetime'))
670+
->will($this->returnValue(0));
671+
672+
/* 2. Initialize dependencies of _cleanup() method which is called second */
673+
$scheduleMock = $this->getMockBuilder('Magento\Cron\Model\Schedule')->disableOriginalConstructor()->getMock();
674+
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection));
675+
$this->_scheduleFactory->expects($this->at(0))->method('create')->will($this->returnValue($scheduleMock));
676+
677+
$collection = $this->getMockBuilder(
678+
'Magento\Cron\Model\Resource\Schedule\Collection'
679+
)->setMethods(
680+
['addFieldToFilter', 'load', '__wakeup']
681+
)->disableOriginalConstructor()->getMock();
682+
$collection->expects($this->any())->method('addFieldToFilter')->will($this->returnSelf());
683+
$collection->expects($this->any())->method('load')->will($this->returnSelf());
684+
$collection->addItem($schedule1);
685+
$collection->addItem($schedule2);
686+
687+
$scheduleMock = $this->getMockBuilder('Magento\Cron\Model\Schedule')->disableOriginalConstructor()->getMock();
688+
$scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($collection));
689+
$this->_scheduleFactory->expects($this->at(1))->method('create')->will($this->returnValue($scheduleMock));
690+
691+
$this->_observer->dispatch('');
692+
}
610693
}

0 commit comments

Comments
 (0)