Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions django_celery_beat/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ def sync(self):
while self._dirty:
name = self._dirty.pop()
try:
self.schedule[name].save()
self._schedule[name].save()
_tried.add(name)
except (KeyError, ObjectDoesNotExist):
except (KeyError, TypeError, ObjectDoesNotExist):
_failed.add(name)
except DatabaseError as exc:
logger.exception('Database error while sync: %r', exc)
Expand Down
22 changes: 22 additions & 0 deletions t/unit/test_schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,28 @@ def test_reserve(self):
assert self.s.flushed == 1
assert self.m2.name in self.s._dirty

def test_sync_not_saves_last_run_at_while_schedule_changed(self):
# Update e1 last_run_at and add to dirty
e1 = self.s.schedule[self.m2.name]
time.sleep(3)
e1.model.last_run_at = e1._default_now()
self.s._dirty.add(e1.model.name)

# Record e1 pre sync last_run_at
e1_pre_sync_last_run_at = e1.model.last_run_at

# Set schedule_changed() == True
self.s._last_timestamp = monotonic()
# Do sync
self.s.sync()

# Record e1 post sync last_run_at
e1_m = PeriodicTask.objects.get(pk=e1.model.pk)
e1_post_sync_last_run_at = e1_m.last_run_at

# Check
assert e1_pre_sync_last_run_at == e1_post_sync_last_run_at

Copy link
Contributor Author

@baijiangjie baijiangjie Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the result of the test. Normally, None should be the last_run_at value updated last time, but after querying it, it is found that it is not.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is normal if self.schedule is changed to self._schedule.

image

def test_sync_saves_last_run_at(self):
e1 = self.s.schedule[self.m2.name]
last_run = e1.last_run_at
Expand Down