Skip to content

Commit

Permalink
fix 'exipres' not working normal as expected (#816)
Browse files Browse the repository at this point in the history
related issuses: 240
  • Loading branch information
x-7 authored Oct 29, 2024
1 parent 7f3ba80 commit 5f0e38a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 8 additions & 1 deletion django_celery_beat/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ def is_due(self):
now = self._default_now()
if getattr(settings, 'DJANGO_CELERY_BEAT_TZ_AWARE', True):
now = maybe_make_aware(self._default_now())

if now < self.model.start_time:
# The datetime is before the start date - don't run.
# send a delay to retry on start_time
Expand All @@ -119,6 +118,14 @@ def is_due(self):
)
return schedules.schedstate(False, delay)

# EXPIRED TASK: Disable task when expired
if self.model.expires is not None:
now = self._default_now()
if now >= self.model.expires:
self._disable(self.model)
# Don't recheck
return schedules.schedstate(False, NEVER_CHECK_TIMEOUT)

# ONE OFF TASK: Disable one off tasks after they've ran once
if self.model.one_off and self.model.enabled \
and self.model.total_run_count > 0:
Expand Down
29 changes: 29 additions & 0 deletions t/unit/test_schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,35 @@ def test_one_off_task(self):
assert not isdue
assert delay == NEVER_CHECK_TIMEOUT

def test_task_with_expires(self):
interval = 10
right_now = self.app.now()
one_second_later = right_now + timedelta(seconds=1)
m = self.create_model_interval(schedule(timedelta(seconds=interval)),
start_time=right_now,
expires=one_second_later)
e = self.Entry(m, app=self.app)
isdue, delay = e.is_due()
assert isdue
assert delay == interval

m2 = self.create_model_interval(schedule(timedelta(seconds=interval)),
start_time=right_now,
expires=right_now)
e2 = self.Entry(m2, app=self.app)
isdue, delay = e2.is_due()
assert not isdue
assert delay == NEVER_CHECK_TIMEOUT

one_second_ago = right_now - timedelta(seconds=1)
m2 = self.create_model_interval(schedule(timedelta(seconds=interval)),
start_time=right_now,
expires=one_second_ago)
e2 = self.Entry(m2, app=self.app)
isdue, delay = e2.is_due()
assert not isdue
assert delay == NEVER_CHECK_TIMEOUT


@pytest.mark.django_db
class test_DatabaseSchedulerFromAppConf(SchedulerCase):
Expand Down

0 comments on commit 5f0e38a

Please sign in to comment.