Skip to content
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

Test cron expressions against localized time #10432

Merged
merged 4 commits into from
Aug 23, 2017

Conversation

AntonEvers
Copy link
Contributor

@AntonEvers AntonEvers commented Aug 4, 2017

Description

As mentioned in #4237:
@AlexanderRakov I do expect it now you mention it, having the current code change in place. But I would not expect it if I were a shop owner. So now that the times are correct in the cron_schedule table, I need to add a change that converts the crontab expression to the correct unix time stamp (UTC) for the matching time in the admin store view time zone.

Fixed Issues (if relevant)

  1. Cron times in the database have a double timezone correction #4237: Cron times in the database have a double timezone correction

Manual testing scenarios

  1. Set the global time zone to something other than GMT.
  2. Schedule the sitemap_generate cron job at 00:00:00.
  3. Configure cron to schedule ahead for 1440
  4. Flush caches
  5. run bin/magento cron:run
  6. Run SELECT scheduled_atFROMcron_scheduleWHEREjob_code = "sitemap_generate";
  7. This should be yyyy-mm-dd 00:00:00 (it was yyyy-mm-dd 00:06:00 in timezone GMT+6 before this PR)

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@ishakhsuvarov ishakhsuvarov self-assigned this Aug 4, 2017
@ishakhsuvarov ishakhsuvarov added this to the August 2017 milestone Aug 4, 2017
@ishakhsuvarov
Copy link
Contributor

Hi @ajpevers
Could you please take a look at the failed unit tests?
Thanks

@AntonEvers
Copy link
Contributor Author

Checks passing. @ishakhsuvarov is this also a valid way to maintain backward compatibility or do you want me to move the object manager fallback back to the constructor and change the unit test?

@ishakhsuvarov
Copy link
Contributor

do you want me to move the object manager fallback back to the constructor

This would be a correct solution according to current guidelines.

@okorshenko okorshenko self-assigned this Aug 8, 2017
$time = $this->getScheduledAt();
$e = $this->getCronExprArr();

if (!$e || !$time) {
return false;
}
if (!is_numeric($time)) {
$time = strtotime($time);
$time = strtotime($time) + $this->dateTime->getGmtOffset();
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not calculate the time for another timezone using inline code. In the future, it will be hard to track all such places if there some bugs. Please, use \Magento\Framework\Stdlib\DateTime\TimezoneInterface to convert to date $time to Default timezone.

@AntonEvers
Copy link
Contributor Author

AntonEvers commented Aug 17, 2017

@okorshenko for a few hours I've been struggling with the unit tests that should be rewritten for my latest change. I'm quite new to this and came a long way, but here I'm stuck. Can you connect me with someone that can help me fix the tests? (It would be nice if we can configure the test with a timezone offset while we're at it.)

*/
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 :'(

*/
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.

@magento-team magento-team merged commit 661824a into magento:develop Aug 23, 2017
magento-team pushed a commit that referenced this pull request Aug 23, 2017
 - fixed timezone conversion logic
 - fixed unit tests
magento-team pushed a commit that referenced this pull request Aug 23, 2017
magento-team pushed a commit that referenced this pull request Aug 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants