Skip to content

Commit 82f1bac

Browse files
author
Anton Evers
committed
Cron uses the wrong timestamp method
Because the cron job uses a localized timestamp instead of a regular timestamp, all times in the cron_schedule table are not the actual times, but times with a double offset. By using the regular timestamp, all times in cron_schedule are correct. The cron job execution times are not affected by this change because schedule generation and job execution depend on the same timestamp. If both are in the same timezone all will go as intended. Fixes #4237
1 parent ec99590 commit 82f1bac

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ class ProcessCronQueueObserver implements ObserverInterface
9797
protected $_shell;
9898

9999
/**
100-
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
100+
* @var \Magento\Framework\Stdlib\DateTime\DateTime
101101
*/
102-
protected $timezone;
102+
protected $dateTime;
103103

104104
/**
105105
* @var \Symfony\Component\Process\PhpExecutableFinder
@@ -124,7 +124,7 @@ class ProcessCronQueueObserver implements ObserverInterface
124124
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
125125
* @param \Magento\Framework\App\Console\Request $request
126126
* @param \Magento\Framework\ShellInterface $shell
127-
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
127+
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
128128
* @param \Magento\Framework\Process\PhpExecutableFinderFactory $phpExecutableFinderFactory
129129
* @param \Psr\Log\LoggerInterface $logger
130130
* @param \Magento\Framework\App\State $state
@@ -138,7 +138,7 @@ public function __construct(
138138
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
139139
\Magento\Framework\App\Console\Request $request,
140140
\Magento\Framework\ShellInterface $shell,
141-
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
141+
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
142142
\Magento\Framework\Process\PhpExecutableFinderFactory $phpExecutableFinderFactory,
143143
\Psr\Log\LoggerInterface $logger,
144144
\Magento\Framework\App\State $state
@@ -150,7 +150,7 @@ public function __construct(
150150
$this->_scopeConfig = $scopeConfig;
151151
$this->_request = $request;
152152
$this->_shell = $shell;
153-
$this->timezone = $timezone;
153+
$this->dateTime = $dateTime;
154154
$this->phpExecutableFinder = $phpExecutableFinderFactory->create();
155155
$this->logger = $logger;
156156
$this->state = $state;
@@ -170,7 +170,7 @@ public function __construct(
170170
public function execute(\Magento\Framework\Event\Observer $observer)
171171
{
172172
$pendingJobs = $this->_getPendingSchedules();
173-
$currentTime = $this->timezone->scopeTimeStamp();
173+
$currentTime = $this->dateTime->gmtTimestamp();
174174
$jobGroupsRoot = $this->_config->getJobs();
175175

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

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

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

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

@@ -322,7 +322,7 @@ protected function _generate($groupId)
322322
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
323323
);
324324
$schedulePeriod = $rawSchedulePeriod * self::SECONDS_IN_MINUTE;
325-
if ($lastRun > $this->timezone->scopeTimeStamp() - $schedulePeriod) {
325+
if ($lastRun > $this->dateTime->gmtTimestamp() - $schedulePeriod) {
326326
return $this;
327327
}
328328

@@ -343,7 +343,7 @@ protected function _generate($groupId)
343343
* save time schedules generation was ran with no expiration
344344
*/
345345
$this->_cache->save(
346-
$this->timezone->scopeTimeStamp(),
346+
$this->dateTime->gmtTimestamp(),
347347
self::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . $groupId,
348348
['crontab'],
349349
null
@@ -398,7 +398,7 @@ protected function _cleanup($groupId)
398398
'system/cron/' . $groupId . '/' . self::XML_PATH_HISTORY_CLEANUP_EVERY,
399399
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
400400
);
401-
if ($lastCleanup > $this->timezone->scopeTimeStamp() - $historyCleanUp * self::SECONDS_IN_MINUTE) {
401+
if ($lastCleanup > $this->dateTime->gmtTimestamp() - $historyCleanUp * self::SECONDS_IN_MINUTE) {
402402
return $this;
403403
}
404404

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

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

444444
// save time history cleanup was ran with no expiration
445445
$this->_cache->save(
446-
$this->timezone->scopeTimeStamp(),
446+
$this->dateTime->gmtTimestamp(),
447447
self::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . $groupId,
448448
['crontab'],
449449
null
@@ -475,7 +475,7 @@ protected function getConfigSchedule($jobConfig)
475475
*/
476476
protected function saveSchedule($jobCode, $cronExpression, $timeInterval, $exists)
477477
{
478-
$currentTime = $this->timezone->scopeTimeStamp();
478+
$currentTime = $this->dateTime->gmtTimestamp();
479479
$timeAhead = $currentTime + $timeInterval;
480480
for ($time = $currentTime; $time < $timeAhead; $time += self::SECONDS_IN_MINUTE) {
481481
$ts = strftime('%Y-%m-%d %H:%M:00', $time);
@@ -503,7 +503,7 @@ protected function generateSchedule($jobCode, $cronExpression, $time)
503503
->setCronExpr($cronExpression)
504504
->setJobCode($jobCode)
505505
->setStatus(Schedule::STATUS_PENDING)
506-
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))
506+
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()))
507507
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $time));
508508

509509
return $schedule;

0 commit comments

Comments
 (0)