Skip to content

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

@codecov
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

@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.

Azurency added a commit to Azurency/django-celery-beat that referenced this pull request Apr 22, 2025
…y#835)"

This reverts commit fc64741.

# Conflicts:
#	django_celery_beat/schedulers.py
#	t/unit/test_schedulers.py
@auvipy
Copy link
Member

auvipy commented Apr 24, 2025

hey, we got report about this PR #875 (comment) can you please check and if possible come with a possible fix? otherwise we have to revert this

@alirafiei75
Copy link
Contributor Author

Oh man, just saw this. Thanks for pointing it out. I've read the comments, and it looks like there's an issue with this PR. I'll definitely look into it and try to come up with a fix. However, I'm currently on a short trip, so it might take until tomorrow. If it's too urgent to wait, feel free to revert for now—I'll make sure to fix it later.

@auvipy
Copy link
Member

auvipy commented Apr 24, 2025

I can wait for your pr

@alirafiei75
Copy link
Contributor Author

alirafiei75 commented Apr 25, 2025

I have made the PR @auvipy . please review it whenever it is convenient

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