Skip to content

Commit

Permalink
fix 'exipres', 'expire_seconds' not working normal as expected
Browse files Browse the repository at this point in the history
related issuses: 240, 592
  • Loading branch information
x-7 committed Oct 23, 2024
1 parent e3c1ade commit e947959
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions django_celery_beat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
17 changes: 14 additions & 3 deletions django_celery_beat/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 \
Expand Down

0 comments on commit e947959

Please sign in to comment.