Skip to content

Commit

Permalink
fix 'exipres' not working normal as expected
Browse files Browse the repository at this point in the history
related issuses: 240
  • Loading branch information
x-7 committed Oct 24, 2024
1 parent e3c1ade commit 9500073
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions django_celery_beat/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ 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 getattr(settings, 'DJANGO_CELERY_BEAT_TZ_AWARE', True):
now = maybe_make_aware(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 9500073

Please sign in to comment.