From e94795952df3d293a583e082eeb5e57d8a13207b Mon Sep 17 00:00:00 2001 From: x-7 Date: Wed, 23 Oct 2024 00:22:59 +0800 Subject: [PATCH] fix 'exipres', 'expire_seconds' not working normal as expected related issuses: 240, 592 --- django_celery_beat/models.py | 4 ++++ django_celery_beat/schedulers.py | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/django_celery_beat/models.py b/django_celery_beat/models.py index 56ed7443..3b6c5889 100644 --- a/django_celery_beat/models.py +++ b/django_celery_beat/models.py @@ -632,6 +632,10 @@ def _clean_expires(self): raise ValidationError( _('Only one can be set, in expires and expire_seconds') ) + if self.expire_seconds is not None and self.start_time is None: + raise ValidationError( + _('Start_time should be set When set expire_seconds') + ) @property def expires_(self): diff --git a/django_celery_beat/schedulers.py b/django_celery_beat/schedulers.py index 846b97a9..197d1774 100644 --- a/django_celery_beat/schedulers.py +++ b/django_celery_beat/schedulers.py @@ -74,9 +74,6 @@ def __init__(self, model, app=None): continue self.options[option] = value - if getattr(model, 'expires_', None): - self.options['expires'] = getattr(model, 'expires_') - self.options['headers'] = loads(model.headers or '{}') self.options['periodic_task_name'] = model.name @@ -118,6 +115,20 @@ def is_due(self): (self.model.start_time - now).total_seconds() ) return schedules.schedstate(False, delay) + + # EXPIRED TASK: Disable task when expired + if self.model.expires_ is not None: + if isinstance(self.model.expires_, int): + expires_t = self.model.start_time + datetime.timedelta(self.model.expires_) + else: + expires_t = self.model.expires_ + + now = self._default_now() + if getattr(settings, 'DJANGO_CELERY_BEAT_TZ_AWARE', True): + now = maybe_make_aware(self._default_now()) + + if now >= expires_t: + self._disable(self.model) # ONE OFF TASK: Disable one off tasks after they've ran once if self.model.one_off and self.model.enabled \