Skip to content

Commit 4d998af

Browse files
authored
Don't set last_run_at to now() when setting DJANGO_CELERY_BEAT_TZ_AWARE is False in ModelEntry save method (#311)
1 parent 92eb551 commit 4d998af

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

django_celery_beat/schedulers.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,6 @@ def save(self):
163163
for field in self.save_fields:
164164
setattr(obj, field, getattr(self.model, field))
165165

166-
if not getattr(settings, 'DJANGO_CELERY_BEAT_TZ_AWARE', True):
167-
obj.last_run_at = datetime.datetime.now()
168-
169166
obj.save()
170167

171168
@classmethod

t/unit/test_schedulers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,40 @@ def test_entry_is_due__no_use_tz(self):
178178
if hasattr(time, "tzset"):
179179
time.tzset()
180180

181+
@override_settings(
182+
USE_TZ=False,
183+
DJANGO_CELERY_BEAT_TZ_AWARE=False
184+
)
185+
@pytest.mark.usefixtures('depends_on_current_app')
186+
@timezone.override('Europe/Berlin')
187+
@pytest.mark.celery(timezone='Europe/Berlin')
188+
def test_entry_and_model_last_run_at_with_utc_no_use_tz(self, monkeypatch):
189+
old_tz = os.environ.get("TZ")
190+
os.environ["TZ"] = "Europe/Berlin"
191+
if hasattr(time, "tzset"):
192+
time.tzset()
193+
assert self.app.timezone.zone == 'Europe/Berlin'
194+
# simulate last_run_at from DB - not TZ aware but localtime
195+
right_now = datetime.utcnow()
196+
# make sure to use fixed date time
197+
monkeypatch.setattr(self.Entry, '_default_now', lambda o: right_now)
198+
m = self.create_model_crontab(
199+
crontab(minute='*/10')
200+
)
201+
m.save()
202+
e = self.Entry(m, app=self.app)
203+
e.save()
204+
m.refresh_from_db()
205+
206+
assert m.last_run_at == e.last_run_at
207+
208+
if old_tz is not None:
209+
os.environ["TZ"] = old_tz
210+
else:
211+
del os.environ["TZ"]
212+
if hasattr(time, "tzset"):
213+
time.tzset()
214+
181215
@override_settings(
182216
USE_TZ=False,
183217
DJANGO_CELERY_BEAT_TZ_AWARE=False,

0 commit comments

Comments
 (0)