Skip to content

Commit 52b4e01

Browse files
milind-shakya-spauvipy
authored andcommitted
Make tz naive and tz aware be configurable (#203)
Add readme Add test settings Add settings import
1 parent d96675d commit 52b4e01

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

README.rst

+7
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ pip command::
229229

230230
$ pip install https://github.com/celery/django-celery-beat/zipball/master#egg=django-celery-beat
231231

232+
233+
TZ Awareness:
234+
-------------
235+
236+
If you have a project that is time zone naive, you can set `DJANGO_CELERY_BEAT_TZ_AWARE=False` in your settings file.
237+
238+
232239
.. |build-status| image:: https://secure.travis-ci.org/celery/django-celery-beat.svg?branch=master
233240
:alt: Build status
234241
:target: https://travis-ci.org/celery/django-celery-beat

django_celery_beat/models.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import timezone_field
77
from celery import schedules
88
from celery.five import python_2_unicode_compatible
9+
from django.conf import settings
910
from django.core.exceptions import MultipleObjectsReturned, ValidationError
1011
from django.core.validators import MaxValueValidator
1112
from django.db import models
@@ -192,13 +193,23 @@ def __str__(self):
192193

193194
@property
194195
def schedule(self):
195-
return TzAwareCrontab(
196+
crontab = schedules.crontab(
196197
minute=self.minute,
197-
hour=self.hour, day_of_week=self.day_of_week,
198+
hour=self.hour,
199+
day_of_week=self.day_of_week,
198200
day_of_month=self.day_of_month,
199201
month_of_year=self.month_of_year,
200-
tz=self.timezone
201202
)
203+
if settings.DJANGO_CELERY_BEAT_TZ_AWARE:
204+
crontab = TzAwareCrontab(
205+
minute=self.minute,
206+
hour=self.hour,
207+
day_of_week=self.day_of_week,
208+
day_of_month=self.day_of_month,
209+
month_of_year=self.month_of_year,
210+
tz=self.timezone
211+
)
212+
return crontab
202213

203214
@classmethod
204215
def from_schedule(cls, schedule):

django_celery_beat/schedulers.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Beat Scheduler Implementation."""
22
from __future__ import absolute_import, unicode_literals
33

4+
import datetime
45
import logging
56

67
from multiprocessing.util import Finalize
@@ -14,6 +15,7 @@
1415
from celery.utils.time import maybe_make_aware
1516
from kombu.utils.json import dumps, loads
1617

18+
from django.conf import settings
1719
from django.db import transaction, close_old_connections
1820
from django.db.utils import DatabaseError, InterfaceError
1921
from django.core.exceptions import ObjectDoesNotExist
@@ -89,7 +91,13 @@ def __init__(self, model, app=None):
8991

9092
if not model.last_run_at:
9193
model.last_run_at = self._default_now()
92-
self.last_run_at = make_aware(model.last_run_at)
94+
95+
last_run_at = model.last_run_at
96+
97+
if settings.DJANGO_CELERY_BEAT_TZ_AWARE:
98+
last_run_at = make_aware(last_run_at)
99+
100+
self.last_run_at = last_run_at
93101

94102
def _disable(self, model):
95103
model.no_changes = True
@@ -125,7 +133,9 @@ def _default_now(self):
125133
# The PyTZ datetime must be localised for the Django-Celery-Beat
126134
# scheduler to work. Keep in mind that timezone arithmatic
127135
# with a localized timezone may be inaccurate.
128-
return now.tzinfo.localize(now.replace(tzinfo=None))
136+
if settings.DJANGO_CELERY_BEAT_TZ_AWARE:
137+
now = now.tzinfo.localize(now.replace(tzinfo=None))
138+
return now
129139

130140
def __next__(self):
131141
self.model.last_run_at = self.app.now()
@@ -140,6 +150,10 @@ def save(self):
140150
obj = type(self.model)._default_manager.get(pk=self.model.pk)
141151
for field in self.save_fields:
142152
setattr(obj, field, getattr(self.model, field))
153+
154+
if not settings.DJANGO_CELERY_BEAT_TZ_AWARE:
155+
obj.last_run_at = datetime.datetime.now()
156+
143157
obj.save()
144158

145159
@classmethod

t/proj/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,4 @@
122122
# https://docs.djangoproject.com/en/1.9/howto/static-files/
123123

124124
STATIC_URL = '/static/'
125+
DJANGO_CELERY_BEAT_TZ_AWARE = True

0 commit comments

Comments
 (0)