-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
) { | ||
parent::__construct($context, $registry, $resource, $resourceCollection, $data); | ||
$this->timeZone = $timeZone; | ||
} | ||
|
||
/** | ||
|
@@ -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) { | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 :'(