Skip to content

Commit aabbaad

Browse files
committed
fix _get_distinct_timezone
the previous query did not actually get distinct timezones
1 parent 673dbc5 commit aabbaad

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

django_celery_beat/schedulers.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@
2323
from kombu.utils.json import dumps, loads
2424

2525
from .clockedschedule import clocked
26-
from .models import (ClockedSchedule, CrontabSchedule, IntervalSchedule,
27-
PeriodicTask, PeriodicTasks, SolarSchedule)
26+
from .models import (
27+
ClockedSchedule,
28+
CrontabSchedule,
29+
IntervalSchedule,
30+
PeriodicTask,
31+
PeriodicTasks,
32+
SolarSchedule,
33+
)
2834
from .utils import NEVER_CHECK_TIMEOUT, aware_now, now
2935

3036
# This scheduler must wake up more frequently than the
@@ -340,7 +346,7 @@ def _get_crontab_exclude_query(self):
340346
+ 24
341347
) % 24
342348
)
343-
for timezone_name in self._get_unique_timezone_names()
349+
for timezone_name in self._get_unique_timezones()
344350
],
345351
# Default case - use hour as is
346352
default=F('hour_int')
@@ -359,11 +365,11 @@ def _get_crontab_exclude_query(self):
359365

360366
return exclude_query
361367

362-
def _get_unique_timezone_names(self):
363-
"""Get a list of all unique timezone names used in CrontabSchedule"""
364-
return CrontabSchedule.objects.values_list(
365-
'timezone', flat=True
366-
).distinct()
368+
def _get_unique_timezones(self):
369+
"""Get a list of all unique timezones used in CrontabSchedule"""
370+
return list(
371+
CrontabSchedule.objects.order_by('timezone').distinct('timezone').values_list('timezone', flat=True)
372+
)
367373

368374
def _get_timezone_offset(self, timezone_name):
369375
"""

t/unit/test_schedulers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,21 @@ def test_crontab_special_hour_four(self):
10591059
# The hour=4 task should never be excluded
10601060
assert task_hour_four.id not in excluded_tasks
10611061

1062+
@pytest.mark.django_db
1063+
def test_get_unique_timezones(self):
1064+
"""
1065+
Test that get unique timezones returns a list of unique timezones
1066+
"""
1067+
# Create 2 crontabs with same timezone, and 1 with different timezone
1068+
CrontabSchedule.objects.create(hour='4', timezone='UTC')
1069+
CrontabSchedule.objects.create(hour='4', timezone='UTC')
1070+
CrontabSchedule.objects.create(hour='4', timezone='America/New_York')
1071+
1072+
timezones = self.s._get_unique_timezones()
1073+
1074+
assert len(timezones) == 2
1075+
assert set(timezones) == {ZoneInfo('UTC'), ZoneInfo('America/New_York')}
1076+
10621077
@pytest.mark.django_db
10631078
@patch('django_celery_beat.schedulers.aware_now')
10641079
@patch('django.utils.timezone.get_current_timezone')

0 commit comments

Comments
 (0)