Skip to content

Commit 18681cf

Browse files
intriprosa
authored andcommitted
Clear clearable jobs in a loop instead of batching
ActiveRecord batching can be slow when looping over records filtered by non-PKs. We don't care about the order of deletion here, so we can just use a loop to delete the clearable jobs; this allows to achieve better performance in general.
1 parent e79b97b commit 18681cf

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

app/models/solid_queue/job/clearable.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ module Clearable
1111

1212
class_methods do
1313
def clear_finished_in_batches(batch_size: 500, finished_before: SolidQueue.clear_finished_jobs_after.ago)
14-
clearable(finished_before: finished_before).in_batches(of: batch_size).delete_all
14+
loop do
15+
records_deleted = clearable(finished_before: finished_before).limit(batch_size).delete_all
16+
break if records_deleted == 0
17+
end
1518
end
1619
end
1720
end

0 commit comments

Comments
 (0)