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 Glib::PurgeUnattached::Config.min_age (8 hours by default) to give upload flows time to attach.

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

Glib::PurgeUnattachedJob.perform_later

Adjust the grace window from the host app (e.g. for a slower upload flow):

Glib::PurgeUnattached::Config.min_age = 10.hours

Instance Method Summary collapse

Instance Method Details

#perform(*_args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'app/jobs/glib/purge_unattached_job.rb', line 20

def perform(*_args)
  scope = ActiveStorage::Blob
    .unattached
    .where('active_storage_blobs.created_at < ?', Glib::PurgeUnattached::Config.min_age.ago)

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

  scope.find_each(&:purge_later)
end