Skip to content

Test cron expressions against localized time #10432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions app/code/Magento/Cron/Model/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,29 @@ class Schedule extends \Magento\Framework\Model\AbstractModel

const STATUS_ERROR = 'error';

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

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface|null $timeZone
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
array $data = [],
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timeZone = null
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be a required argument (to avoid the object manager hack below). Move it up to go before the optional ones.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding new required argument to the class constructor will break backwards compatibility, for those third-party extensions which could have extended from it.
Please refer to the Backwards Compatible Development Guide for more information (this case is covered in Adding a constructor parameter section)

Copy link

@sshymko sshymko Aug 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ishakhsuvarov
Good to know. The BC policy seems very decremental to the codebase :'(

) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
$this->timeZone = $timeZone;
}

/**
Expand Down Expand Up @@ -94,7 +102,14 @@ public function setCronExpr($expr)
*/
public function trySchedule()
{
$time = $this->getScheduledAt();
$time = $this->getDateTime()->formatDateTime(
$this->getScheduledAt(),
\IntlDateFormatter::NONE,
\IntlDateFormatter::NONE,
null,
null,
'yyyy-MM-dd HH:mm:ss'
);
$e = $this->getCronExprArr();

if (!$e || !$time) {
Expand Down Expand Up @@ -241,4 +256,15 @@ public function tryLockJob()
}
return false;
}

/**
* @deprecated
* @return \Magento\Framework\Stdlib\DateTime\TimezoneInterface
*/
private function getDateTime()
{
$this->timeZone = $this->timeZone ?: \Magento\Framework\App\ObjectManager::getInstance()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static access to the object manager is highly discouraged. The interface primarily exists for the unserialization use case. It should not be used outside of that. The object manager will not be necessary if to inject the dependency as required on constructor. See the constructor comment above.

->get(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
return $this->timeZone;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Magento\Framework\App\State;
use Magento\Framework\Console\Cli;
use Magento\Framework\Event\ObserverInterface;
use \Magento\Cron\Model\Schedule;
use Magento\Cron\Model\Schedule;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down