Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: avoid initializing schedules.crontab #687

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions django_celery_beat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,23 +341,23 @@ def __str__(self):

@property
def schedule(self):
crontab = schedules.crontab(
minute=self.minute,
hour=self.hour,
day_of_week=self.day_of_week,
day_of_month=self.day_of_month,
month_of_year=self.month_of_year,
)
if getattr(settings, 'DJANGO_CELERY_BEAT_TZ_AWARE', True):
crontab = TzAwareCrontab(
return TzAwareCrontab(
minute=self.minute,
hour=self.hour,
day_of_week=self.day_of_week,
day_of_month=self.day_of_month,
month_of_year=self.month_of_year,
tz=self.timezone
)
return crontab

return schedules.crontab(
minute=self.minute,
hour=self.hour,
day_of_week=self.day_of_week,
day_of_month=self.day_of_month,
month_of_year=self.month_of_year,
)

@classmethod
def from_schedule(cls, schedule):
Expand Down
15 changes: 15 additions & 0 deletions t/unit/test_schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from django_celery_beat.models import (DAYS, ClockedSchedule, CrontabSchedule,
IntervalSchedule, PeriodicTask,
PeriodicTasks, SolarSchedule)
from django_celery_beat.tzcrontab import TzAwareCrontab
from django_celery_beat.utils import NEVER_CHECK_TIMEOUT, make_aware

_ids = count(0)
Expand Down Expand Up @@ -668,6 +669,20 @@ def test_CrontabSchedule_unicode(self):
month_of_year='4,6',
)) == '3 3 */2 4,6 tue (m/h/dM/MY/d) UTC'

@override_settings(
DJANGO_CELERY_BEAT_TZ_AWARE=False
)
def test_CrontabSchedule_schedule_crontab(self):
s = CrontabSchedule()
assert isinstance(s.schedule, crontab)

@override_settings(
DJANGO_CELERY_BEAT_TZ_AWARE=True
)
def test_CrontabSchedule_schedule_TzAwareCrontab(self):
s = CrontabSchedule()
assert isinstance(s.schedule, TzAwareCrontab)

def test_PeriodicTask_unicode_interval(self):
p = self.create_model_interval(schedule(timedelta(seconds=10)))
assert str(p) == f'{p.name}: every 10.0 seconds'
Expand Down