Skip to content

Commit bec8f15

Browse files
authored
fix: simplify task fetching by removing crontab exclusion #956
Removed crontab exclusion logic from task fetching. Filters were created to limit window to +/-2 hours for crontab, but tasks are never refetched - must either implement refetching protocol or (as implemented here) remove the filter entirely.
1 parent 09b5fe9 commit bec8f15

File tree

1 file changed

+1
-68
lines changed

1 file changed

+1
-68
lines changed

django_celery_beat/schedulers.py

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -284,75 +284,8 @@ def enabled_models_qs(self):
284284
clocked__isnull=False,
285285
clocked__clocked_time__gt=next_schedule_sync
286286
)
287-
288-
exclude_cron_tasks_query = self._get_crontab_exclude_query()
289-
290-
# Combine the queries for optimal database filtering
291-
exclude_query = exclude_clock_tasks_query | exclude_cron_tasks_query
292-
293287
# Fetch only the tasks we need to consider
294-
return self.Model.objects.enabled().exclude(exclude_query)
295-
296-
def _get_crontab_exclude_query(self):
297-
"""
298-
Build a query to exclude crontab tasks based on their hour value,
299-
adjusted for timezone differences relative to the server.
300-
301-
This creates an annotation for each crontab task that represents the
302-
server-equivalent hour, then filters on that annotation.
303-
"""
304-
# Get server time based on Django settings
305-
306-
server_time = aware_now()
307-
server_hour = server_time.hour
308-
309-
# Window of +/- 2 hours around the current hour in server tz.
310-
hours_to_include = [
311-
(server_hour + offset) % 24 for offset in range(-2, 3)
312-
]
313-
hours_to_include += [4] # celery's default cleanup task
314-
315-
# Get all tasks with a simple numeric hour value
316-
valid_numeric_hours = self._get_valid_hour_formats()
317-
numeric_hour_tasks = CrontabSchedule.objects.filter(
318-
hour__in=valid_numeric_hours
319-
)
320-
321-
# Annotate these tasks with their server-hour equivalent
322-
annotated_tasks = numeric_hour_tasks.annotate(
323-
# Cast hour string to integer
324-
hour_int=Cast('hour', IntegerField()),
325-
326-
# Calculate server-hour based on timezone offset
327-
server_hour=Case(
328-
# Handle each timezone specifically
329-
*[
330-
When(
331-
timezone=timezone_name,
332-
then=(
333-
F('hour_int')
334-
+ self._get_timezone_offset(timezone_name)
335-
+ 24
336-
) % 24
337-
)
338-
for timezone_name in self._get_unique_timezone_names()
339-
],
340-
# Default case - use hour as is
341-
default=F('hour_int')
342-
)
343-
)
344-
345-
excluded_hour_task_ids = annotated_tasks.exclude(
346-
server_hour__in=hours_to_include
347-
).values_list('id', flat=True)
348-
349-
# Build the final exclude query:
350-
# Exclude crontab tasks that are not in our include list
351-
exclude_query = Q(crontab__isnull=False) & Q(
352-
crontab__id__in=excluded_hour_task_ids
353-
)
354-
355-
return exclude_query
288+
return self.Model.objects.enabled().exclude(exclude_clock_tasks_query)
356289

357290
def _get_valid_hour_formats(self):
358291
"""

0 commit comments

Comments
 (0)