Skip to content

Commit 2555bba

Browse files
authored
Start time heap block fix (#636)
* Update schedulers.py * Update test_schedulers.py * Update schedulers.py * Update test_schedulers.py * Update schedulers.py * Update schedulers.py * Update test_schedulers.py * Update schedulers.py
1 parent 8f9fd1b commit 2555bba

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

django_celery_beat/schedulers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ def __init__(self, model, app=None):
8585

8686
if not model.last_run_at:
8787
model.last_run_at = self._default_now()
88+
# if last_run_at is not set and
89+
# model.start_time last_run_at should be in way past.
90+
# This will trigger the job to run at start_time
91+
# and avoid the heap block.
92+
if self.model.start_time:
93+
model.last_run_at = model.last_run_at \
94+
- datetime.timedelta(days=365 * 30)
8895

8996
self.last_run_at = model.last_run_at
9097

t/unit/test_schedulers.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,29 @@ def test_heap_always_return_the_first_item(self):
599599
s.sync()
600600
assert len(tried) == 1 and tried == {e1.name}
601601

602+
def test_starttime_trigger(self, monkeypatch):
603+
# Ensure there is no heap block in case of new task with start_time
604+
PeriodicTask.objects.all().delete()
605+
s = self.Scheduler(app=self.app)
606+
assert not s._heap
607+
m1 = self.create_model_interval(schedule(timedelta(seconds=3)))
608+
m1.save()
609+
s.tick()
610+
assert len(s._heap) == 2
611+
m2 = self.create_model_interval(
612+
schedule(timedelta(days=1)),
613+
start_time=make_aware(
614+
datetime.now() + timedelta(seconds=2)))
615+
m2.save()
616+
s.tick()
617+
assert s._heap[0][2].name == m2.name
618+
assert len(s._heap) == 3
619+
assert s._heap[0]
620+
time.sleep(2)
621+
s.tick()
622+
assert s._heap[0]
623+
assert s._heap[0][2].name == m1.name
624+
602625

603626
@pytest.mark.django_db()
604627
class test_models(SchedulerCase):

0 commit comments

Comments
 (0)