Class: ActiveStorageDedup::DeduplicationJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/active_storage_dedup/deduplication_job.rb

Instance Method Summary collapse

Instance Method Details

#performObject

Sanity check job to find and merge duplicate blobs across the entire database Can be run on-demand or scheduled (daily/weekly) to clean up any duplicates that may have slipped through due to race conditions

Examples:

Run manually

ActiveStorageDedup::DeduplicationJob.perform_now

Schedule with whenever gem

every 1.day, at: '2:00 am' do
  runner "ActiveStorageDedup::DeduplicationJob.perform_later"
end

Schedule with sidekiq-cron

ActiveStorageDedup::DeduplicationJob.set(cron: '0 2 * * *').perform_later


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_storage_dedup/deduplication_job.rb', line 21

def perform
  Rails.logger.info "[ActiveStorageDedup] 🔍 Starting sanity check - scanning for duplicate blobs..."

  # Find all checksum+service combinations that have duplicates
  duplicate_groups = ActiveStorage::Blob
                     .select(:checksum, :service_name)
                     .group(:checksum, :service_name)
                     .having("COUNT(*) > 1")
                     .count

  if duplicate_groups.empty?
    Rails.logger.info "[ActiveStorageDedup] ✓ No duplicate blobs found - database is clean!"
    return
  end

  Rails.logger.info "[ActiveStorageDedup] Found #{duplicate_groups.size} group(s) with duplicates"

  total_merged = 0
  duplicate_groups.each_key do |(checksum, service_name)|
    merged = process_duplicate_group(checksum, service_name)
    total_merged += merged
  end

  Rails.logger.info "[ActiveStorageDedup] ✓ Sanity check complete - merged #{total_merged} duplicate blob(s)"
end