|
1 | 1 | """Beat Scheduler Implementation."""
|
| 2 | +from __future__ import annotations |
| 3 | + |
2 | 4 | import datetime
|
3 | 5 | import logging
|
4 | 6 | import math
|
|
15 | 17 | from celery.utils.time import maybe_make_aware
|
16 | 18 | from django.conf import settings
|
17 | 19 | from django.core.exceptions import ObjectDoesNotExist
|
18 |
| -from django.db import close_old_connections, transaction |
| 20 | +from django.db import close_old_connections, connection, transaction |
19 | 21 | from django.db.models import Case, F, IntegerField, Q, When
|
20 | 22 | from django.db.models.functions import Cast
|
21 | 23 | from django.db.utils import DatabaseError, InterfaceError
|
@@ -337,7 +339,7 @@ def _get_crontab_exclude_query(self):
|
337 | 339 | + 24
|
338 | 340 | ) % 24
|
339 | 341 | )
|
340 |
| - for timezone_name in self._get_unique_timezone_names() |
| 342 | + for timezone_name in self._get_unique_timezones() |
341 | 343 | ],
|
342 | 344 | # Default case - use hour as is
|
343 | 345 | default=F('hour_int')
|
@@ -365,11 +367,25 @@ def _get_valid_hour_formats(self):
|
365 | 367 | f"{hour:02d}" for hour in range(10)
|
366 | 368 | ]
|
367 | 369 |
|
368 |
| - def _get_unique_timezone_names(self): |
369 |
| - """Get a list of all unique timezone names used in CrontabSchedule""" |
370 |
| - return CrontabSchedule.objects.values_list( |
371 |
| - 'timezone', flat=True |
372 |
| - ).distinct() |
| 370 | + def _get_unique_timezones(self) -> set[ZoneInfo]: |
| 371 | + """Get a set of all unique timezones used in CrontabSchedule""" |
| 372 | + # sqlite does not support distinct on a column |
| 373 | + if connection.vendor == 'sqlite': |
| 374 | + return set( |
| 375 | + CrontabSchedule.objects.values_list( |
| 376 | + 'timezone', flat=True |
| 377 | + ) |
| 378 | + ) |
| 379 | + |
| 380 | + return set( |
| 381 | + CrontabSchedule.objects.order_by( |
| 382 | + 'timezone' |
| 383 | + ).distinct( |
| 384 | + 'timezone' |
| 385 | + ).values_list( |
| 386 | + 'timezone', flat=True |
| 387 | + ) |
| 388 | + ) |
373 | 389 |
|
374 | 390 | def _get_timezone_offset(self, timezone_name):
|
375 | 391 | """
|
|
0 commit comments