Optimize beat reloads for far-future clocked tasks#1041
Open
diaxoaine wants to merge 5 commits into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1041 +/- ##
==========================================
+ Coverage 87.68% 87.95% +0.27%
==========================================
Files 32 32
Lines 1015 1038 +23
Branches 81 85 +4
==========================================
+ Hits 890 913 +23
Misses 107 107
Partials 18 18 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR reduces unnecessary PeriodicTasks “last_change” updates (and thus DatabaseScheduler reloads) when creating new clocked schedules/tasks that are sufficiently far in the future, relying on the scheduler’s periodic full sync instead.
Changes:
- Add sync-deadline helpers (
next_schedule_sync_at/by) and expose scheduler sync interval constants viautils. - Skip
PeriodicTasks.update_changed()on creation of far-futureClockedSchedulerows and far-future createdPeriodicTaskrows with a clocked schedule. - Add unit tests covering in-window vs out-of-window change tracking, including configured
beat_max_loop_interval.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
t/unit/test_schedulers.py |
Adds tests validating when clocked creations/updates should (or should not) bump PeriodicTasks.last_change(). |
django_celery_beat/utils.py |
Centralizes scheduler interval constants and adds helpers to compute next sync timing/deadline. |
django_celery_beat/signals.py |
Replaces ClockedSchedule post-save handler with a conditional version that may skip forced reloads. |
django_celery_beat/schedulers.py |
Uses the new helper for computing the next full-sync cutoff for excluding far-future clocked tasks. |
django_celery_beat/models.py |
Passes a created flag into PeriodicTasks.changed() and conditionally skips change tracking for far-future created clocked tasks. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
ClockedScheduleis often used for dynamically created one-off tasks. In high-load apps, these schedules may be created very frequently, which can lead to frequentPeriodicTasksupdates and cause beat to reload from the database more often than necessary, potentially as often as every 5 seconds.This PR avoids unnecessary
PeriodicTaskschange updates for newly created clocked schedules/tasks that are scheduled far enough in the future.