Skip to content

Commit ab2b5be

Browse files
asfaltboyauvipy
authored andcommitted
fix: invalidate heap to an empty list and not None (#166)
The issue occurs in celery 4.2+ and the included test case reproduces it, introduced in pull celery/celery#4493 and initially introduced in PR #25 - on initial read, return None to make sure we populate heap and old_schedulers - include (hypothetic) test case where every sync returns an update - inverse _initial_read condition to be make it clearer fixes celery/celery#5032
1 parent 1bbf4ab commit ab2b5be

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

django_celery_beat/schedulers.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class DatabaseScheduler(Scheduler):
192192

193193
_schedule = None
194194
_last_timestamp = None
195-
_initial_read = False
195+
_initial_read = True
196196

197197
def __init__(self, *args, **kwargs):
198198
"""Initialize the database scheduler."""
@@ -294,11 +294,11 @@ def install_default_entries(self, data):
294294

295295
@property
296296
def schedule(self):
297-
update = False
298-
if not self._initial_read:
297+
initial = update = False
298+
if self._initial_read:
299299
debug('DatabaseScheduler: initial read')
300-
update = True
301-
self._initial_read = True
300+
initial = update = True
301+
self._initial_read = False
302302
elif self.schedule_changed():
303303
info('DatabaseScheduler: Schedule changed.')
304304
update = True
@@ -307,7 +307,8 @@ def schedule(self):
307307
self.sync()
308308
self._schedule = self.all_as_schedule()
309309
# the schedule changed, invalidate the heap in Scheduler.tick
310-
self._heap = None
310+
if not initial:
311+
self._heap = []
311312
if logger.isEnabledFor(logging.DEBUG):
312313
debug('Current schedule:\n%s', '\n'.join(
313314
repr(entry) for entry in values(self._schedule)),

t/unit/test_schedulers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ def test_sync_rollback_on_save_error(self):
400400
with pytest.raises(RuntimeError):
401401
self.s.sync()
402402

403+
def test_update_scheduler_heap_invalidation(self, monkeypatch):
404+
# mock "schedule_changed" to always trigger update for
405+
# all calls to schedule, as a change may occur at any moment
406+
monkeypatch.setattr(self.s, 'schedule_changed', lambda: True)
407+
self.s.tick()
408+
403409

404410
@pytest.mark.django_db()
405411
class test_models(SchedulerCase):

0 commit comments

Comments
 (0)