File tree Expand file tree Collapse file tree 2 files changed +31
-8
lines changed Expand file tree Collapse file tree 2 files changed +31
-8
lines changed Original file line number Diff line number Diff line change 2323from kombu .utils .json import dumps , loads
2424
2525from .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+ )
2834from .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 """
Original file line number Diff line number Diff line change @@ -1059,6 +1059,23 @@ 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_crontab_special_hour_four (self ):
1064+ """
1065+ Test that schedules with hour=4 are always included, regardless of
1066+ the current time.
1067+ """
1068+ # Create 2 crontabs with same timezone, and 1 with different timezone
1069+ CrontabSchedule .objects .create (hour = '4' , timezone = 'UTC' )
1070+ CrontabSchedule .objects .create (hour = '4' , timezone = 'UTC' )
1071+ CrontabSchedule .objects .create (hour = '4' , timezone = 'America/New_York' )
1072+
1073+ # Run the scheduler's exclusion logic
1074+ timezones = self .s ._get_unique_timezones ()
1075+
1076+ assert len (timezones ) == 2
1077+ assert set (timezones ) == {ZoneInfo ('UTC' ), ZoneInfo ('America/New_York' )}
1078+
10621079 @pytest .mark .django_db
10631080 @patch ('django_celery_beat.schedulers.aware_now' )
10641081 @patch ('django.utils.timezone.get_current_timezone' )
You can’t perform that action at this time.
0 commit comments