Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 'exipres', 'expire_seconds' not working normal as expected #816

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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