Skip to content

Commit

Permalink
GMT timestamps for Magento 2.2.0, fixes #296
Browse files Browse the repository at this point in the history
Since Magento 2.2.0 the cron database tables will use GMT timestamps,
before that version Magento has used timestamps in a different timezone.

Fix is to branch based on Magento version:

a) Use GMT timestamps from version 2.2.0 or above.

b) Use local timestamps up to version 2.2.0

Refs:

- Command: sys:cron:run

- Command: sys:cron:schedule

- #296

- magento/magento2#9943
  • Loading branch information
ktomk authored and cmuench committed Oct 11, 2017
1 parent 2c03a2d commit dbc2442
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ RECENT CHANGES

1.5.1
-----
* Imp: Extract timestamp creation for cron commands (by Tom Klingenberg, #296)
* Fix: GMT timestamps for Magento 2.2.0 (by Tom Klingenberg, #296)

1.5.0
-----
Expand Down
25 changes: 24 additions & 1 deletion src/N98/Magento/Command/System/Cron/AbstractCronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,48 @@ abstract class AbstractCronCommand extends AbstractMagentoCommand
*/
protected $cronScheduleCollection;

/**
* @var \Magento\Framework\App\ProductMetadataInterface $productMetadata
*/
private $productMetadata;

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

/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
private $dateTime;

/**
* @param \Magento\Framework\App\State $state
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Cron\Model\ConfigInterface $cronConfig
* @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Cron\Model\ResourceModel\Schedule\Collection $cronScheduleCollection
*/
public function inject(
\Magento\Framework\App\State $state,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Cron\Model\ConfigInterface $cronConfig,
\Magento\Framework\App\ProductMetadataInterface $productMetadata,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Cron\Model\ResourceModel\Schedule\Collection $cronScheduleCollection
) {
$this->state = $state;
$this->cronConfig = $cronConfig;
$this->scopeConfig = $scopeConfig;
$this->cronScheduleCollection = $cronScheduleCollection;
$this->productMetadata = $productMetadata;
$this->timezone = $timezone;
$this->dateTime = $dateTime;
}

/**
Expand Down Expand Up @@ -203,12 +219,19 @@ protected function getJobForExecuteMethod(InputInterface $input, OutputInterface
* Get timestamp used for time related database fields in the cron tables
*
* Note: The timestamp used will change from Magento 2.1.7 to 2.2.0 and
* these changes can be branched on Magento version in this method.
* these changes are branched by Magento version in this method.
*
* @return int
*/
protected function getCronTimestamp()
{
/* @var $version string e.g. "2.1.7" */
$version = $this->productMetadata->getVersion();

if (version_compare($version, "2.2.0") >= 0) {
return $this->dateTime->gmtTimestamp();
}

return $this->timezone->scopeTimeStamp();
}
}

0 comments on commit dbc2442

Please sign in to comment.