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

Merged
merged 5 commits into from
Jan 29, 2025

Conversation

alirafiei75
Copy link
Contributor

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.42%. Comparing base (17d87f4) to head (7b9b0d2).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #835      +/-   ##
==========================================
+ Coverage   87.20%   87.42%   +0.21%     
==========================================
  Files          32       32              
  Lines         938      954      +16     
  Branches       76       76              
==========================================
+ Hits          818      834      +16     
  Misses        102      102              
  Partials       18       18              

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

@alirafiei75
Copy link
Contributor 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
@auvipy auvipy merged commit fc64741 into celery:main Jan 29, 2025
25 checks passed
@auvipy
Copy link
Member

auvipy commented Jan 29, 2025

thanks for your work. feel free to contribute back if you find any better improvement or regression.

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