Skip to content

Commit 26100b8

Browse files
alirafiei75auvipy
andauthored
refactor: use in operator instead of regex operator in crontab query (#900)
* refactor: use __in operator instead of __regex operator in crontab exclusion query * Update django_celery_beat/schedulers.py * fix: valid hours list * Update django_celery_beat/schedulers.py * fix: pre commit error * test: add test for valid numeric hours --------- Co-authored-by: Asif Saif Uddin <[email protected]>
1 parent 673dbc5 commit 26100b8

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

django_celery_beat/schedulers.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,10 @@ def _get_crontab_exclude_query(self):
314314
]
315315
hours_to_include += [4] # celery's default cleanup task
316316

317-
# Regex pattern to match only numbers
318-
# This ensures we only process numeric hour values
319-
numeric_hour_pattern = r'^\d+$'
320-
321317
# Get all tasks with a simple numeric hour value
318+
valid_numeric_hours = self._get_valid_hour_formats()
322319
numeric_hour_tasks = CrontabSchedule.objects.filter(
323-
hour__regex=numeric_hour_pattern
320+
hour__in=valid_numeric_hours
324321
)
325322

326323
# Annotate these tasks with their server-hour equivalent
@@ -359,6 +356,15 @@ def _get_crontab_exclude_query(self):
359356

360357
return exclude_query
361358

359+
def _get_valid_hour_formats(self):
360+
"""
361+
Return a list of all valid hour values (0-23).
362+
Both zero-padded ("00"–"09") and non-padded ("0"–"23")
363+
"""
364+
return [str(hour) for hour in range(24)] + [
365+
f"{hour:02d}" for hour in range(10)
366+
]
367+
362368
def _get_unique_timezone_names(self):
363369
"""Get a list of all unique timezone names used in CrontabSchedule"""
364370
return CrontabSchedule.objects.values_list(

t/unit/test_schedulers.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,41 @@ def test_crontab_timezone_conversion_with_negative_offset_and_dst(
12121212
"Paris outside window task should be excluded"
12131213
)
12141214

1215+
def test_scheduler_valid_hours(self):
1216+
"""Test the _get_valid_hour_formats method."""
1217+
# Create an instance of DatabaseScheduler
1218+
s = self.Scheduler(app=self.app)
1219+
1220+
# Get the valid hours list
1221+
valid_hours = s._get_valid_hour_formats()
1222+
1223+
# Basic validations
1224+
# 24 regular hours + 10 zero-padded hours
1225+
assert len(valid_hours) == 34
1226+
1227+
# Check both regular and zero-padded formats are present
1228+
assert "0" in valid_hours
1229+
assert "00" in valid_hours
1230+
assert "9" in valid_hours
1231+
assert "09" in valid_hours
1232+
assert "23" in valid_hours
1233+
1234+
# Verify all hours 0-23 are included
1235+
for hour in range(24):
1236+
assert str(hour) in valid_hours
1237+
1238+
# Verify zero-padded hours 00-09 are included
1239+
for hour in range(10):
1240+
assert f"{hour:02d}" in valid_hours
1241+
1242+
# Check for duplicates (set should have same length as list)
1243+
assert len(set(valid_hours)) == len(valid_hours)
1244+
1245+
# Verify all entries are valid hour representations
1246+
for hour_str in valid_hours:
1247+
hour_value = int(hour_str)
1248+
assert 0 <= hour_value <= 23
1249+
12151250

12161251
@pytest.mark.django_db
12171252
class test_models(SchedulerCase):

0 commit comments

Comments
 (0)