Skip to content

Commit cf6efc9

Browse files
committed
Add a clear_in_batches method to delete recurring executions
This is useful for those who decide not to have FKs that ensure recurring executions are deleted when jobs are cleared up, so they can just call this method periodically to clear orphaned executions.
1 parent de9910a commit cf6efc9

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

app/models/solid_queue/recurring_execution.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22

33
module SolidQueue
44
class RecurringExecution < Execution
5-
def self.record(task_key, run_at, &block)
6-
transaction do
7-
if job_id = block.call
8-
create!(job_id: job_id, task_key: task_key, run_at: run_at)
5+
scope :clearable, -> { where.missing(:job) }
6+
7+
class << self
8+
def record(task_key, run_at, &block)
9+
transaction do
10+
if job_id = block.call
11+
create!(job_id: job_id, task_key: task_key, run_at: run_at)
12+
end
13+
end
14+
rescue ActiveRecord::RecordNotUnique
15+
SolidQueue.logger.info("[SolidQueue] Skipped recurring task #{task_key} at #{run_at} — already dispatched")
16+
end
17+
18+
def clear_in_batches(batch_size: 500)
19+
loop do
20+
records_deleted = clearable.limit(batch_size).delete_all
21+
break if records_deleted == 0
922
end
1023
end
11-
rescue ActiveRecord::RecordNotUnique
12-
SolidQueue.logger.info("[SolidQueue] Skipped recurring task #{task_key} at #{run_at} — already dispatched")
1324
end
1425
end
1526
end

0 commit comments

Comments
 (0)