Skip to content

Commit

Permalink
Make tz naive and tz aware be configurable (#203)
Browse files Browse the repository at this point in the history
Add readme

Add test settings

Add settings import
  • Loading branch information
milind-shakya-sp authored and auvipy committed Dec 4, 2018
1 parent d96675d commit 52b4e01
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ pip command::

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


TZ Awareness:
-------------

If you have a project that is time zone naive, you can set `DJANGO_CELERY_BEAT_TZ_AWARE=False` in your settings file.


.. |build-status| image:: https://secure.travis-ci.org/celery/django-celery-beat.svg?branch=master
:alt: Build status
:target: https://travis-ci.org/celery/django-celery-beat
Expand Down
17 changes: 14 additions & 3 deletions django_celery_beat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import timezone_field
from celery import schedules
from celery.five import python_2_unicode_compatible
from django.conf import settings
from django.core.exceptions import MultipleObjectsReturned, ValidationError
from django.core.validators import MaxValueValidator
from django.db import models
Expand Down Expand Up @@ -192,13 +193,23 @@ def __str__(self):

@property
def schedule(self):
return TzAwareCrontab(
crontab = schedules.crontab(
minute=self.minute,
hour=self.hour, day_of_week=self.day_of_week,
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
)
if settings.DJANGO_CELERY_BEAT_TZ_AWARE:
crontab = 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

@classmethod
def from_schedule(cls, schedule):
Expand Down
18 changes: 16 additions & 2 deletions django_celery_beat/schedulers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Beat Scheduler Implementation."""
from __future__ import absolute_import, unicode_literals

import datetime
import logging

from multiprocessing.util import Finalize
Expand All @@ -14,6 +15,7 @@
from celery.utils.time import maybe_make_aware
from kombu.utils.json import dumps, loads

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

if not model.last_run_at:
model.last_run_at = self._default_now()
self.last_run_at = make_aware(model.last_run_at)

last_run_at = model.last_run_at

if settings.DJANGO_CELERY_BEAT_TZ_AWARE:
last_run_at = make_aware(last_run_at)

self.last_run_at = last_run_at

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

def __next__(self):
self.model.last_run_at = self.app.now()
Expand All @@ -140,6 +150,10 @@ def save(self):
obj = type(self.model)._default_manager.get(pk=self.model.pk)
for field in self.save_fields:
setattr(obj, field, getattr(self.model, field))

if not settings.DJANGO_CELERY_BEAT_TZ_AWARE:
obj.last_run_at = datetime.datetime.now()

obj.save()

@classmethod
Expand Down
1 change: 1 addition & 0 deletions t/proj/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'
DJANGO_CELERY_BEAT_TZ_AWARE = True

0 comments on commit 52b4e01

Please sign in to comment.