Skip to content

Commit 9500073

Browse files
committed
fix 'exipres' not working normal as expected
related issuses: 240
1 parent e3c1ade commit 9500073

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

django_celery_beat/schedulers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ def is_due(self):
119119
)
120120
return schedules.schedstate(False, delay)
121121

122+
# EXPIRED TASK: Disable task when expired
123+
if self.model.expires is not None:
124+
now = self._default_now()
125+
if getattr(settings, 'DJANGO_CELERY_BEAT_TZ_AWARE', True):
126+
now = maybe_make_aware(self._default_now())
127+
128+
if now >= self.model.expires:
129+
self._disable(self.model)
130+
# Don't recheck
131+
return schedules.schedstate(False, NEVER_CHECK_TIMEOUT)
132+
122133
# ONE OFF TASK: Disable one off tasks after they've ran once
123134
if self.model.one_off and self.model.enabled \
124135
and self.model.total_run_count > 0:

t/unit/test_schedulers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,35 @@ def test_one_off_task(self):
291291
assert not isdue
292292
assert delay == NEVER_CHECK_TIMEOUT
293293

294+
def test_task_with_expires(self):
295+
interval = 10
296+
right_now = self.app.now()
297+
one_second_later = right_now + timedelta(seconds=1)
298+
m = self.create_model_interval(schedule(timedelta(seconds=interval)),
299+
start_time=right_now,
300+
expires=one_second_later)
301+
e = self.Entry(m, app=self.app)
302+
isdue, delay = e.is_due()
303+
assert isdue
304+
assert delay == interval
305+
306+
m2 = self.create_model_interval(schedule(timedelta(seconds=interval)),
307+
start_time=right_now,
308+
expires=right_now)
309+
e2 = self.Entry(m2, app=self.app)
310+
isdue, delay = e2.is_due()
311+
assert not isdue
312+
assert delay == NEVER_CHECK_TIMEOUT
313+
314+
one_second_ago = right_now - timedelta(seconds=1)
315+
m2 = self.create_model_interval(schedule(timedelta(seconds=interval)),
316+
start_time=right_now,
317+
expires=one_second_ago)
318+
e2 = self.Entry(m2, app=self.app)
319+
isdue, delay = e2.is_due()
320+
assert not isdue
321+
assert delay == NEVER_CHECK_TIMEOUT
322+
294323

295324
@pytest.mark.django_db
296325
class test_DatabaseSchedulerFromAppConf(SchedulerCase):

0 commit comments

Comments
 (0)