Skip to content

Commit 16dfff1

Browse files
izimobilauvipy
authored andcommitted
Fixed delay returned when a task has a start_time in the future. (#208)
When a task has a start_time in the future, the scheduler now returns a delay that corresponds to the number of seconds from the current date to the start_time. This should fix issue #195, at least partially.
1 parent faa2089 commit 16dfff1

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

django_celery_beat/schedulers.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import datetime
55
import logging
6+
import math
67

78
from multiprocessing.util import Finalize
89

@@ -111,10 +112,13 @@ def is_due(self):
111112

112113
# START DATE: only run after the `start_time`, if one exists.
113114
if self.model.start_time is not None:
114-
if maybe_make_aware(self._default_now()) < self.model.start_time:
115+
now = maybe_make_aware(self._default_now())
116+
if now < self.model.start_time:
115117
# The datetime is before the start date - don't run.
116-
_, delay = self.schedule.is_due(self.last_run_at)
117-
# use original delay for re-check
118+
# send a delay to retry on start_time
119+
delay = math.ceil(
120+
(self.model.start_time - now).total_seconds()
121+
)
118122
return schedules.schedstate(False, delay)
119123

120124
# ONE OFF TASK: Disable one off tasks after they've ran once

t/unit/test_schedulers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import, unicode_literals
22

3+
import math
34
import time
45
import pytest
56

@@ -151,7 +152,7 @@ def test_task_with_start_time(self):
151152
e2 = self.Entry(m2, app=self.app)
152153
isdue, delay = e2.is_due()
153154
assert not isdue
154-
assert delay == interval
155+
assert delay == math.ceil((tomorrow - right_now).total_seconds())
155156

156157
def test_one_off_task(self):
157158
interval = 10

0 commit comments

Comments
 (0)