Skip to content

Commit

Permalink
MAGETWO-69886: Cron uses the wrong timestamp method #9943
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Korshenko authored Jun 15, 2017
2 parents 397f87b + 91b0630 commit b33f359
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
30 changes: 15 additions & 15 deletions app/code/Magento/Cron/Observer/ProcessCronQueueObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ class ProcessCronQueueObserver implements ObserverInterface
protected $_shell;

/**
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
protected $timezone;
protected $dateTime;

/**
* @var \Symfony\Component\Process\PhpExecutableFinder
Expand All @@ -124,7 +124,7 @@ class ProcessCronQueueObserver implements ObserverInterface
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Framework\App\Console\Request $request
* @param \Magento\Framework\ShellInterface $shell
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
* @param \Magento\Framework\Process\PhpExecutableFinderFactory $phpExecutableFinderFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\App\State $state
Expand All @@ -138,7 +138,7 @@ public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\App\Console\Request $request,
\Magento\Framework\ShellInterface $shell,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
\Magento\Framework\Process\PhpExecutableFinderFactory $phpExecutableFinderFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\App\State $state
Expand All @@ -150,7 +150,7 @@ public function __construct(
$this->_scopeConfig = $scopeConfig;
$this->_request = $request;
$this->_shell = $shell;
$this->timezone = $timezone;
$this->dateTime = $dateTime;
$this->phpExecutableFinder = $phpExecutableFinderFactory->create();
$this->logger = $logger;
$this->state = $state;
Expand All @@ -170,7 +170,7 @@ public function __construct(
public function execute(\Magento\Framework\Event\Observer $observer)
{
$pendingJobs = $this->_getPendingSchedules();
$currentTime = $this->timezone->scopeTimeStamp();
$currentTime = $this->dateTime->gmtTimestamp();
$jobGroupsRoot = $this->_config->getJobs();

$phpPath = $this->phpExecutableFinder->find() ?: 'php';
Expand Down Expand Up @@ -274,7 +274,7 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
);
}

$schedule->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))->save();
$schedule->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()))->save();

try {
call_user_func_array($callback, [$schedule]);
Expand All @@ -285,7 +285,7 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,

$schedule->setStatus(Schedule::STATUS_SUCCESS)->setFinishedAt(strftime(
'%Y-%m-%d %H:%M:%S',
$this->timezone->scopeTimeStamp()
$this->dateTime->gmtTimestamp()
));
}

Expand Down Expand Up @@ -322,7 +322,7 @@ protected function _generate($groupId)
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$schedulePeriod = $rawSchedulePeriod * self::SECONDS_IN_MINUTE;
if ($lastRun > $this->timezone->scopeTimeStamp() - $schedulePeriod) {
if ($lastRun > $this->dateTime->gmtTimestamp() - $schedulePeriod) {
return $this;
}

Expand All @@ -343,7 +343,7 @@ protected function _generate($groupId)
* save time schedules generation was ran with no expiration
*/
$this->_cache->save(
$this->timezone->scopeTimeStamp(),
$this->dateTime->gmtTimestamp(),
self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . $groupId,
['crontab'],
null
Expand Down Expand Up @@ -398,7 +398,7 @@ protected function _cleanup($groupId)
'system/cron/' . $groupId . '/' . self::XML_PATH_HISTORY_CLEANUP_EVERY,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
if ($lastCleanup > $this->timezone->scopeTimeStamp() - $historyCleanUp * self::SECONDS_IN_MINUTE) {
if ($lastCleanup > $this->dateTime->gmtTimestamp() - $historyCleanUp * self::SECONDS_IN_MINUTE) {
return $this;
}

Expand Down Expand Up @@ -431,7 +431,7 @@ protected function _cleanup($groupId)
Schedule::STATUS_ERROR => $historyFailure * self::SECONDS_IN_MINUTE,
];

$now = $this->timezone->scopeTimeStamp();
$now = $this->dateTime->gmtTimestamp();
/** @var Schedule $record */
foreach ($history as $record) {
$checkTime = $record->getExecutedAt() ? strtotime($record->getExecutedAt()) :
Expand All @@ -443,7 +443,7 @@ protected function _cleanup($groupId)

// save time history cleanup was ran with no expiration
$this->_cache->save(
$this->timezone->scopeTimeStamp(),
$this->dateTime->gmtTimestamp(),
self::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . $groupId,
['crontab'],
null
Expand Down Expand Up @@ -475,7 +475,7 @@ protected function getConfigSchedule($jobConfig)
*/
protected function saveSchedule($jobCode, $cronExpression, $timeInterval, $exists)
{
$currentTime = $this->timezone->scopeTimeStamp();
$currentTime = $this->dateTime->gmtTimestamp();
$timeAhead = $currentTime + $timeInterval;
for ($time = $currentTime; $time < $timeAhead; $time += self::SECONDS_IN_MINUTE) {
$ts = strftime('%Y-%m-%d %H:%M:00', $time);
Expand Down Expand Up @@ -503,7 +503,7 @@ protected function generateSchedule($jobCode, $cronExpression, $time)
->setCronExpr($cronExpression)
->setJobCode($jobCode)
->setStatus(Schedule::STATUS_PENDING)
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()))
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $time));

return $schedule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ class ProcessCronQueueObserverTest extends \PHPUnit_Framework_TestCase
protected $_cronGroupConfig;

/**
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
protected $timezone;
protected $dateTimeMock;

/**
* @var \Magento\Framework\Event\Observer
Expand Down Expand Up @@ -126,8 +126,10 @@ protected function setUp()

$this->observer = $this->getMock(\Magento\Framework\Event\Observer::class, [], [], '', false);

$this->timezone = $this->getMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
$this->timezone->expects($this->any())->method('scopeTimeStamp')->will($this->returnValue(time()));
$this->dateTimeMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
->disableOriginalConstructor()
->getMock();
$this->dateTimeMock->expects($this->any())->method('gmtTimestamp')->will($this->returnValue(time()));

$phpExecutableFinder = $this->getMock(\Symfony\Component\Process\PhpExecutableFinder::class, [], [], '', false);
$phpExecutableFinder->expects($this->any())->method('find')->willReturn('php');
Expand All @@ -148,7 +150,7 @@ protected function setUp()
$this->_scopeConfig,
$this->_request,
$this->_shell,
$this->timezone,
$this->dateTimeMock,
$phpExecutableFinderFactory,
$this->loggerMock,
$this->appStateMock
Expand Down

0 comments on commit b33f359

Please sign in to comment.