Skip to content

Commit

Permalink
Use batch queries in GoodJob::self.cleanup_preserved_jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Apr 18, 2023
1 parent 8dacdf5 commit 14c2751
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 16 additions & 9 deletions lib/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,29 @@ def self._shutdown_all(executables, method_name = :shutdown, timeout: -1)
# destroy old records and preserve space in your database.
# @param older_than [nil,Numeric,ActiveSupport::Duration] Jobs older than this will be destroyed (default: +86400+).
# @return [Integer] Number of job execution records and batches that were destroyed.
def self.cleanup_preserved_jobs(older_than: nil)
def self.cleanup_preserved_jobs(older_than: nil, in_batches_of: 1_000)
older_than ||= GoodJob.configuration.cleanup_preserved_jobs_before_seconds_ago
timestamp = Time.current - older_than
include_discarded = GoodJob.configuration.cleanup_discarded_jobs?

ActiveSupport::Notifications.instrument("cleanup_preserved_jobs.good_job", { older_than: older_than, timestamp: timestamp }) do |payload|
old_jobs = GoodJob::Job.where('finished_at <= ?', timestamp)
old_jobs = old_jobs.succeeded unless include_discarded
deleted_executions_count = GoodJob::Execution.where(job: old_jobs).delete_all
deleted_executions_count = 0
deleted_batches_count = 0

jobs_query = GoodJob::Job.where('finished_at <= ?', timestamp)
jobs_query = jobs_query.succeeded unless include_discarded

jobs_query.in_batches(of: in_batches_of).each do |relation|
deleted_executions_count += GoodJob::Execution.where(job: relation).delete_all
end

if GoodJob::BatchRecord.migrated?
old_batches = GoodJob::BatchRecord.where('finished_at <= ?', timestamp)
old_batches = old_batches.succeeded unless include_discarded
deleted_batches_count = old_batches.delete_all
else
deleted_batches_count = 0
batches_query = GoodJob::BatchRecord.where('finished_at <= ?', timestamp)
batches_query = batches_query.succeeded unless include_discarded

batches_query.in_batches(of: in_batches_of).each do |relation|
deleted_batches_count += relation.delete_all
end
end

payload[:destroyed_executions_count] = deleted_executions_count
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/good_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
let!(:old_batch) { GoodJob::BatchRecord.create!(finished_at: 15.days.ago) }

it 'deletes finished jobs' do
destroyed_records_count = described_class.cleanup_preserved_jobs
destroyed_records_count = described_class.cleanup_preserved_jobs(in_batches_of: 1)

expect(destroyed_records_count).to eq 4

Expand Down

0 comments on commit 14c2751

Please sign in to comment.