Skip to content

Fix task ETA issues when timezone is defined in configuration - #3867

Merged
thedrow merged 4 commits into
celery:masterfrom
georgepsarakis:bug/3753_retry_delay_utc_timezone
Mar 2, 2017
Merged

thedrow merged 4 commits into
celery:masterfrom
georgepsarakis:bug/3753_retry_delay_utc_timezone

Conversation

@georgepsarakis

Copy link
Copy Markdown
Contributor

Description

  • Modified App.now to correctly return the timezone-aware current date/time, according to the configured timezone or UTC.
  • Added unit tests for the changed/added methods.
  • Optimize tests by reducing wait time for some test cases due to default polling interval value.

Fixes #3753.

SebastianBerchtold pushed a commit to smartlane/celery that referenced this pull request Jun 11, 2018
…#3867)

* Fix wrong task ETA when using timezone setting

* Optimization: reduce polling interval in test cases

* Test cases for App.now, App.uses_utc_timezone

* Fix PEP8 and pydocstyle issues
Comment thread celery/app/base.py
"""Return the current time and date as a datetime."""
return self.loader.now(utc=self.conf.enable_utc)
from datetime import datetime
return datetime.utcnow().replace(tzinfo=self.timezone)

@Chronial Chronial Aug 12, 2018 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think this is wrong. This will not return the current time in self.timezone. Let's say for example that self.timezone is +8 and it is currently 12:00 UTC. This method will return 12:00 +8, which is the same point in time as 04:00 UTC – not 12:00 UTC.
Note that this current implementation might even return nonsensical times, like 2017-03-25 02:30 for a Europe/Berlin timezone – a time that doesn't exist.

The correct way to get the current time in a local timezone is datetime.utcnow().replace(tzinfo=pytz.utc).astimezone(self.timezone). See the same functionality in django and the pytz docs.

But you can't just make a function that used to return utc times return times from timezones that observe DST. If the caller code is not aware of that, things might break. Example: You return 2017-03-24 03:30 Europe/Berlin. The caller adds 24h to this and then checks if the time is before 04:00. Adding 24h crosses a DST boundary, so the above +24h is actually 2017-03-25 04:30. But if you add 24h to a 2017-03-24 03:30 Europe/Berlin datetime object, you get 2017-03-25 03:30 Europe/Berlin [NON-DST]. You need to call timezone.normalize on that to turn that into a sensible datetime object – see the pytz docs.

Hope this helps.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Chronial would you mind sending a pr with your suggestions?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Providing a correct implementation using timezones is not easy. Since I moved away from using celery (its broken timzone handling beeing one of the reasons ^^), I unfortunately do not have the time to provide a full PR for this.

Just wanted to report this bug, since I stumbled upon it while browsing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Chronial you are right, this has already been corrected by other contributors in master/latest release.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ah, sorry for digging up this old PR. I came here via the mention on #4006 and didn't realize how old this PR was.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No problem @Chronial thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants