Skip to content

Commit

Permalink
Extract timestamp creation for cron commands, #296
Browse files Browse the repository at this point in the history
Preparation for upcoming changes in Magento 2.2.0.

Note: There is no branch on version yet, this needs future
      implementation.

Refs:

- #296
  • Loading branch information
ktomk authored and cmuench committed Oct 11, 2017
1 parent aaa163b commit 2c03a2d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ RECENT CHANGES

1.5.1
-----
* Imp: Extract timestamp creation for cron commands (by Tom Klingenberg, #296)

1.5.0
-----
Expand Down
13 changes: 13 additions & 0 deletions src/N98/Magento/Command/System/Cron/AbstractCronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,17 @@ protected function getJobForExecuteMethod(InputInterface $input, OutputInterface

return array($jobCode, $jobConfig, $model);
}

/**
* 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.
*
* @return int
*/
protected function getCronTimestamp()
{
return $this->timezone->scopeTimeStamp();
}
}
8 changes: 4 additions & 4 deletions src/N98/Magento/Command/System/Cron/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
$schedule
->setJobCode($jobCode)
->setStatus(Schedule::STATUS_RUNNING)
->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))
->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', $this->getCronTimestamp()))
->save();

try {
$this->state->emulateAreaCode(Area::AREA_CRONTAB, $callback, array($schedule));

$schedule
->setStatus(Schedule::STATUS_SUCCESS)
->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))
->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', $this->getCronTimestamp()))
->save();
} catch (Exception $e) {
$schedule
->setStatus(Schedule::STATUS_ERROR)
->setMessages($e->getMessage())
->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))
->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', $this->getCronTimestamp()))
->save();
}

if (isset($e)) {
throw new RuntimeException(
sprintf('Cron-job "%s" threw exception %s', $jobCode, get_class($e)),
Expand Down
2 changes: 1 addition & 1 deletion src/N98/Magento/Command/System/Cron/ScheduleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
'<info>Scheduling </info><comment>' . $jobConfig['instance'] . '::' . $jobConfig['method'] . '</comment> '
);

$createdAtTime = $this->timezone->scopeTimeStamp();
$createdAtTime = $this->getCronTimestamp();
$scheduledAtTime = $createdAtTime;

/* @var $schedule \Magento\Cron\Model\Schedule */
Expand Down

0 comments on commit 2c03a2d

Please sign in to comment.