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

Add method, index and FKs to help with clearing finished jobs #52

Merged
merged 2 commits into from
Dec 7, 2023

Conversation

rosa
Copy link
Member

@rosa rosa commented Dec 6, 2023

This is a follow-up to #44, and it flips the setting to delete jobs to preserve_finished_jobs, which is true by default, so jobs will be kept by default instead. It also adds a new setting to indicate how long preserved jobs should be kept and a method to make the task of clearing finished jobs easier.

This also adds some foreign keys in the execution tables, that might help as a failsafe, to ensure that no executions are left behind when jobs are deleted.

And preserve them by default. Also, add a new option to indicate when
to delete preserved jobs.

class_methods do
def clear_finished_in_batches(batch_size: 500, finished_before: SolidQueue.clear_finished_jobs_after.ago)
where(finished_at: ...finished_before).in_batches(of: batch_size, &:delete_all)
Copy link
Contributor

Choose a reason for hiding this comment

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

Small tip: you can call delete_all directly on the batch enumerator:

in_batches(of: batch_size).delete_all

https://api.rubyonrails.org/classes/ActiveRecord/Batches/BatchEnumerator.html#method-i-delete_all

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, nice! Definitely reads nicer! 🙏

@rosa rosa force-pushed the clear-finished-jobs branch from 0443fc7 to f1a6319 Compare December 6, 2023 15:52
…time

The foreign keys with cascade delete are just a failsafe, to ensure we don't
fail to clear executions after deleting jobs.
@rosa rosa force-pushed the clear-finished-jobs branch from f1a6319 to ec3fde0 Compare December 7, 2023 12:42
@rosa rosa merged commit 75b08ba into main Dec 7, 2023
@rosa rosa deleted the clear-finished-jobs branch December 7, 2023 12:58
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.

2 participants