From 9501e7062c19c6861dedf332930643ffffb2e16c Mon Sep 17 00:00:00 2001 From: chentt <7448203+daydaychen@users.noreply.github.com> Date: Fri, 19 Jan 2024 14:10:04 +0800 Subject: [PATCH 1/4] fix long period task will never be triggered --- django_celery_beat/schedulers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_celery_beat/schedulers.py b/django_celery_beat/schedulers.py index 846b97a9..810152fc 100644 --- a/django_celery_beat/schedulers.py +++ b/django_celery_beat/schedulers.py @@ -84,7 +84,7 @@ def __init__(self, model, app=None): self.model = model if not model.last_run_at: - model.last_run_at = self._default_now() + model.last_run_at = model.date_changed # if last_run_at is not set and # model.start_time last_run_at should be in way past. # This will trigger the job to run at start_time From 8a94a48c09afbc1e1769b5d1503d47d44110bf3b Mon Sep 17 00:00:00 2001 From: chentiantian Date: Fri, 14 Jun 2024 10:54:21 +0800 Subject: [PATCH 2/4] fix (schedulers): when model.date_changed is None, use _default.now() --- django_celery_beat/schedulers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_celery_beat/schedulers.py b/django_celery_beat/schedulers.py index 810152fc..25f932d6 100644 --- a/django_celery_beat/schedulers.py +++ b/django_celery_beat/schedulers.py @@ -84,7 +84,7 @@ def __init__(self, model, app=None): self.model = model if not model.last_run_at: - model.last_run_at = model.date_changed + model.last_run_at = model.date_changed or self._default_now() # if last_run_at is not set and # model.start_time last_run_at should be in way past. # This will trigger the job to run at start_time From b3a989fcf4e0e5ad2b3f2f18ee6fa2b6a81f4b00 Mon Sep 17 00:00:00 2001 From: chentiantian Date: Fri, 14 Jun 2024 10:55:11 +0800 Subject: [PATCH 3/4] test (schedulers): add test for when model.last_run_at is None --- t/unit/test_schedulers.py | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/t/unit/test_schedulers.py b/t/unit/test_schedulers.py index dbafe666..a6b1d8a0 100644 --- a/t/unit/test_schedulers.py +++ b/t/unit/test_schedulers.py @@ -213,6 +213,55 @@ def test_entry_and_model_last_run_at_with_utc_no_use_tz(self, monkeypatch): if hasattr(time, "tzset"): time.tzset() + @override_settings( + USE_TZ=False, + DJANGO_CELERY_BEAT_TZ_AWARE=False + ) + @pytest.mark.usefixtures('depends_on_current_app') + @timezone.override('Europe/Berlin') + @pytest.mark.celery(timezone='Europe/Berlin') + def test_entry_and_model_last_run_at_when_model_changed(self, monkeypatch): + old_tz = os.environ.get("TZ") + os.environ["TZ"] = "Europe/Berlin" + if hasattr(time, "tzset"): + time.tzset() + assert self.app.timezone.key == 'Europe/Berlin' + # simulate last_run_at from DB - not TZ aware but localtime + right_now = datetime.utcnow() + # make sure to use fixed date time + monkeypatch.setattr(self.Entry, '_default_now', lambda o: right_now) + m = self.create_model_crontab( + crontab(minute='*/10') + ) + m.save() + e = self.Entry(m, app=self.app) + e.save() + m.refresh_from_db() + + # The just created model has no value for last_run_at + # so last_run_at should be set to the Entry._default_now() + assert m.last_run_at == e.last_run_at + + # If the model has been updated and the entry.last_run_at is None, + # entry.last_run_at should be set to the value of model.date_changed. + # see #717 + m2 = self.create_model_crontab( + crontab(minute='*/10') + ) + m2.save() + m2.refresh_from_db() + assert m2.date_changed is not None + e2 = self.Entry(m2, app=self.app) + e2.save() + assert m2.last_run_at == m2.date_changed + + if old_tz is not None: + os.environ["TZ"] = old_tz + else: + del os.environ["TZ"] + if hasattr(time, "tzset"): + time.tzset() + @override_settings( USE_TZ=False, DJANGO_CELERY_BEAT_TZ_AWARE=False, From 96c25ee897dd1d7b4b96595df88fd4aeda74f29c Mon Sep 17 00:00:00 2001 From: chentiantian Date: Sat, 15 Jun 2024 09:30:02 +0800 Subject: [PATCH 4/4] test (scheduler): revise the comment --- t/unit/test_schedulers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/unit/test_schedulers.py b/t/unit/test_schedulers.py index a6b1d8a0..2ff2ea4f 100644 --- a/t/unit/test_schedulers.py +++ b/t/unit/test_schedulers.py @@ -238,7 +238,7 @@ def test_entry_and_model_last_run_at_when_model_changed(self, monkeypatch): e.save() m.refresh_from_db() - # The just created model has no value for last_run_at + # The just created model has no value for date_changed # so last_run_at should be set to the Entry._default_now() assert m.last_run_at == e.last_run_at