Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(db-scheduler): optimize all_as_schedule query #835

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

alirafiei75
Copy link

Summary

This PR optimizes the all_as_schedule function to improve performance by filtering unnecessary tasks from the database query.

Changes

  • Exclude clocked tasks not due within 5 minutes.
  • Exclude crontab tasks outside the relevant hour range.

fixes #675
fixes #786

Copy link

codecov bot commented Jan 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.40%. Comparing base (17d87f4) to head (0205cdf).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #835      +/-   ##
==========================================
+ Coverage   87.20%   87.40%   +0.20%     
==========================================
  Files          32       32              
  Lines         938      953      +15     
  Branches       76       76              
==========================================
+ Hits          818      833      +15     
  Misses        102      102              
  Partials       18       18              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@alirafiei75
Copy link
Author

Screenshot from 2025-01-04 12-04-45

This is cpu usage of our celery-beat server. We depend highly on cronjobs and clockjobs and as you see, optimizing the scheduler query, led to reduction in cpu usage considerably (see 11:53 in the picture)

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

The changes look good from the first review. I will thoroughly Review it again in a couple of days before merging

django_celery_beat/schedulers.py Show resolved Hide resolved
django_celery_beat/schedulers.py Show resolved Hide resolved
@daydaychen
Copy link
Contributor

I have also encountered performance issues when there are a large number of scheduled tasks. I addressed this in a straightforward manner by adding a last_pull and pull_interval attribute. Within the schedule_changed method, when a change is detected, I check whether the conditions between last_pull and pull_interval are met. This reduces the number of queries and avoids repeated pulls during frequent updates.

However, it is evident that alirafiei75's solution is more ingenious. It not only addresses the performance issues but also effectively reduces the pressure on database queries.

@alirafiei75 alirafiei75 requested a review from auvipy January 16, 2025 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DatabaseScheduler is bad for a large amount of tasks? Scheduler is slow when dealing with lots of tasks
3 participants