Skip to content

Commit 700c804

Browse files
authored
Fix Issue #388: Celery Beat scheduled tasks may be executed repeatedly (#660)
* Fix: The problem that Celery Beat scheduled tasks may be executed repeatedly * fix: 增加 TypeError 捕获 * fix: Add tests for `test_sync_not_saves_last_run_at_while_schedule_changed`
1 parent 5132379 commit 700c804

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

django_celery_beat/schedulers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ def sync(self):
300300
while self._dirty:
301301
name = self._dirty.pop()
302302
try:
303-
self.schedule[name].save()
303+
self._schedule[name].save()
304304
_tried.add(name)
305-
except (KeyError, ObjectDoesNotExist):
305+
except (KeyError, TypeError, ObjectDoesNotExist):
306306
_failed.add(name)
307307
except DatabaseError as exc:
308308
logger.exception('Database error while sync: %r', exc)

t/unit/test_schedulers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,28 @@ def test_reserve(self):
444444
assert self.s.flushed == 1
445445
assert self.m2.name in self.s._dirty
446446

447+
def test_sync_not_saves_last_run_at_while_schedule_changed(self):
448+
# Update e1 last_run_at and add to dirty
449+
e1 = self.s.schedule[self.m2.name]
450+
time.sleep(3)
451+
e1.model.last_run_at = e1._default_now()
452+
self.s._dirty.add(e1.model.name)
453+
454+
# Record e1 pre sync last_run_at
455+
e1_pre_sync_last_run_at = e1.model.last_run_at
456+
457+
# Set schedule_changed() == True
458+
self.s._last_timestamp = monotonic()
459+
# Do sync
460+
self.s.sync()
461+
462+
# Record e1 post sync last_run_at
463+
e1_m = PeriodicTask.objects.get(pk=e1.model.pk)
464+
e1_post_sync_last_run_at = e1_m.last_run_at
465+
466+
# Check
467+
assert e1_pre_sync_last_run_at == e1_post_sync_last_run_at
468+
447469
def test_sync_saves_last_run_at(self):
448470
e1 = self.s.schedule[self.m2.name]
449471
last_run = e1.last_run_at

0 commit comments

Comments
 (0)