-
Notifications
You must be signed in to change notification settings - Fork 428
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
Fix Issue #604: Celery Beat Crashing at the End of Daylight Savings #605
Open
polarmt
wants to merge
11
commits into
celery:main
Choose a base branch
from
polarmt:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a9d66a9
Add is_dst parameter based on time's tm_isdst field instead of passin…
b0749b2
add unit tests
e73f397
Add -1 case
d142f88
add line
6d51ddc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 789d16b
format
6df332f
more formatting changes
0a1fe8b
some more format optimizations
c54d3c1
try this
a4a39c0
shorter line
f723a74
Merge branch 'main' into master
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
import time | ||
from datetime import datetime | ||
from unittest import mock | ||
|
||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
from django_celery_beat import utils | ||
|
||
|
||
class UtilsTest(TestCase): | ||
|
||
@mock.patch('django_celery_beat.utils.timezone.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone') | ||
@mock.patch('django_celery_beat.utils.timezone.make_aware') | ||
@mock.patch('django_celery_beat.utils.time.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.is_naive') | ||
@mock.patch('django_celery_beat.utils.getattr') | ||
def test_make_aware_use_tz_naive(self, | ||
mock_getattr, | ||
mock_is_naive, | ||
mock_localtime_1, | ||
mock_make_aware, | ||
mock_get_default_timezone, | ||
mock_localtime_2): | ||
dt = datetime(2022, 11, 6, 1, 15, 0) | ||
mock_getattr.return_value = True | ||
mock_is_naive.return_value = True | ||
mock_get_default_timezone.return_value = "America/Los_Angeles" | ||
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0] | ||
mock_localtime_2.return_value = time.struct_time(test_time) | ||
mock_make_aware.return_value = dt | ||
|
||
self.assertEqual(utils.make_aware(dt), mock_localtime_2.return_value) | ||
|
||
mock_localtime_1.assert_not_called() | ||
mock_make_aware.assert_called_with(dt, timezone.utc) | ||
mock_get_default_timezone.assert_called() | ||
mock_localtime_2.assert_called_with(dt, | ||
"America/Los_Angeles") | ||
|
||
@mock.patch('django_celery_beat.utils.timezone.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone') | ||
@mock.patch('django_celery_beat.utils.timezone.make_aware') | ||
@mock.patch('django_celery_beat.utils.time.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.is_naive') | ||
@mock.patch('django_celery_beat.utils.getattr') | ||
def test_make_aware_use_tz_not_naive(self, | ||
mock_getattr, | ||
mock_is_naive, | ||
mock_localtime_1, | ||
mock_make_aware, | ||
mock_get_default_timezone, | ||
mock_localtime_2): | ||
dt = datetime(2022, 11, 6, 1, 15, 0) | ||
mock_getattr.return_value = True | ||
mock_is_naive.return_value = False | ||
mock_get_default_timezone.return_value = "America/Los_Angeles" | ||
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0] | ||
mock_localtime_2.return_value = time.struct_time(test_time) | ||
mock_make_aware.return_value = dt | ||
|
||
self.assertEqual(utils.make_aware(dt), mock_localtime_2.return_value) | ||
|
||
mock_localtime_1.assert_not_called() | ||
mock_make_aware.assert_not_called() | ||
mock_get_default_timezone.assert_called() | ||
mock_localtime_2.assert_called_with(dt, | ||
"America/Los_Angeles") | ||
|
||
@mock.patch('django_celery_beat.utils.timezone.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone') | ||
@mock.patch('django_celery_beat.utils.timezone.make_aware') | ||
@mock.patch('django_celery_beat.utils.time.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.is_naive') | ||
@mock.patch('django_celery_beat.utils.getattr') | ||
def test_make_aware_not_use_tz_naive_dst(self, | ||
mock_getattr, | ||
mock_is_naive, | ||
mock_localtime_1, | ||
mock_make_aware, | ||
mock_get_default_timezone, | ||
mock_localtime_2): | ||
dt = datetime(2022, 11, 6, 1, 15, 0) | ||
mock_getattr.return_value = False | ||
mock_is_naive.return_value = True | ||
mock_get_default_timezone.return_value = "America/Los_Angeles" | ||
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 1] | ||
mock_localtime_1.return_value = time.struct_time(test_time) | ||
mock_make_aware.return_value = dt | ||
|
||
self.assertEqual(utils.make_aware(dt), dt) | ||
|
||
mock_localtime_1.assert_called_with() | ||
mock_make_aware.assert_called_with(dt, | ||
"America/Los_Angeles", | ||
is_dst=True) | ||
mock_get_default_timezone.assert_called() | ||
mock_localtime_2.assert_not_called() | ||
|
||
@mock.patch('django_celery_beat.utils.timezone.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone') | ||
@mock.patch('django_celery_beat.utils.timezone.make_aware') | ||
@mock.patch('django_celery_beat.utils.time.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.is_naive') | ||
@mock.patch('django_celery_beat.utils.getattr') | ||
def test_make_aware_not_use_tz_naive_not_dst(self, | ||
mock_getattr, | ||
mock_is_naive, | ||
mock_localtime_1, | ||
mock_make_aware, | ||
mock_get_default_timezone, | ||
mock_localtime_2): | ||
dt = datetime(2022, 11, 6, 1, 15, 0) | ||
mock_getattr.return_value = False | ||
mock_is_naive.return_value = True | ||
mock_get_default_timezone.return_value = "America/Los_Angeles" | ||
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0] | ||
mock_localtime_1.return_value = time.struct_time(test_time) | ||
mock_make_aware.return_value = dt | ||
|
||
self.assertEqual(utils.make_aware(dt), dt) | ||
|
||
mock_localtime_1.assert_called_with() | ||
mock_make_aware.assert_called_with(dt, | ||
"America/Los_Angeles", | ||
is_dst=False) | ||
mock_get_default_timezone.assert_called() | ||
mock_localtime_2.assert_not_called() | ||
|
||
@mock.patch('django_celery_beat.utils.timezone.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone') | ||
@mock.patch('django_celery_beat.utils.timezone.make_aware') | ||
@mock.patch('django_celery_beat.utils.time.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.is_naive') | ||
@mock.patch('django_celery_beat.utils.getattr') | ||
def test_make_aware_not_use_tz_naive_neg_dst(self, | ||
mock_getattr, | ||
mock_is_naive, | ||
mock_localtime_1, | ||
mock_make_aware, | ||
mock_get_default_timezone, | ||
mock_localtime_2): | ||
dt = datetime(2022, 11, 6, 1, 15, 0) | ||
mock_getattr.return_value = False | ||
mock_is_naive.return_value = True | ||
mock_get_default_timezone.return_value = "America/Los_Angeles" | ||
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, -1] | ||
mock_localtime_1.return_value = time.struct_time(test_time) | ||
mock_make_aware.return_value = dt | ||
|
||
self.assertEqual(utils.make_aware(dt), dt) | ||
|
||
mock_localtime_1.assert_called_with() | ||
mock_make_aware.assert_called_with(dt, | ||
"America/Los_Angeles", | ||
is_dst=None) | ||
mock_get_default_timezone.assert_called() | ||
|
||
@mock.patch('django_celery_beat.utils.timezone.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone') | ||
@mock.patch('django_celery_beat.utils.timezone.make_aware') | ||
@mock.patch('django_celery_beat.utils.time.localtime') | ||
@mock.patch('django_celery_beat.utils.timezone.is_naive') | ||
@mock.patch('django_celery_beat.utils.getattr') | ||
def test_make_aware_not_use_tz_not_naive_dst(self, | ||
mock_getattr, | ||
mock_is_naive, | ||
mock_localtime_1, | ||
mock_make_aware, | ||
mock_get_default_timezone, | ||
mock_localtime_2): | ||
dt = datetime(2022, 11, 6, 1, 15, 0) | ||
mock_getattr.return_value = False | ||
mock_is_naive.return_value = False | ||
mock_get_default_timezone.return_value = "America/Los_Angeles" | ||
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0] | ||
mock_localtime_1.return_value = time.struct_time(test_time) | ||
mock_make_aware.return_value = dt | ||
|
||
self.assertEqual(utils.make_aware(dt), dt) | ||
|
||
mock_localtime_1.assert_not_called() | ||
mock_make_aware.assert_not_called() | ||
mock_get_default_timezone.assert_not_called() | ||
mock_localtime_2.assert_not_called() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I am not sure about this part, can any other way we can check it?
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.
I referenced these posts 1 (top answer only) and 2. There are other suggested solutions online. I can summarize the problems with these solutions.
Use of
localize
Source 1
Source 2
Source 3
The root cause was inside
localize
. Using thelocalize
to fix the problem withlocalize
sounds counterintuitive. For instance, link 1 mentions the following in the final solution:time.daylight
Source
This solution might work, but the underlying code is just to do a similar check to what I do here. This code is cleaner, and I can change it if you would like.
Calculations
Source
One way is to calculate the timezone based on the current time and a previous time, but I think that this solution is long and too ad-hoc when we can achieve this with a few lines of code.
Conclusion
I think that
time.daylight
ortime.localtime
+tm_isdst
is the best way to go for this. If you believe in the other solutions, please let me know.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.
I have to check to give you proper answer
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.
Sounds good. Let me know when you've checked.