Class: Glib::PurgeUnattachedJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/glib/purge_unattached_job.rb

Overview

Daily cleanup for ActiveStorage blobs that have no attachments. Excludes blobs referenced by any SnapshotBlobReference row, so old file versions that still appear in some snapshot's timeline (i.e. a previous version of an attached file) survive the purge.

Opt-in: if the host app has not run the gem's create_snapshot_blob_references migration, SnapshotBlobReference.table_exists? is false and the exclusion subquery short-circuits — the job then behaves identically to Rails' built-in unattached-blob cleanup, just delayed by 8 hours to give upload flows time to attach.

Schedule from the host app (e.g. via sidekiq-cron):

Glib::PurgeUnattachedJob.perform_later

Instance Method Summary collapse

Instance Method Details

#perform(*_args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'app/jobs/glib/purge_unattached_job.rb', line 16

def perform(*_args)
  scope = ActiveStorage::Blob
    .unattached
    .where('active_storage_blobs.created_at < ?', 8.hours.ago)

  if SnapshotBlobReference.table_exists?
    scope = scope.where.not(id: SnapshotBlobReference.select(:blob_id))
  end

  scope.find_each(&:purge_later)
end