Class: Lato::ActiveStorageCleanerJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/lato/active_storage_cleaner_job.rb

Instance Method Summary collapse

Instance Method Details

#performObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/jobs/lato/active_storage_cleaner_job.rb', line 3

def perform
  # delete all active storage blobs that are not attached to any record with more than 12 hours of life
  ActiveStorage::Blob.unattached.where('active_storage_blobs.created_at < ?', 12.hours.ago).find_in_batches do |blobs|
    blobs.each(&:purge_later)
  end

  # delete all empty folders in active storage local service (if exists)
  if ActiveStorage::Blob.service.class.to_s == 'ActiveStorage::Service::DiskService'
    storage_folder = ActiveStorage::Blob.service.root
    return unless File.directory?(storage_folder)

    Dir.foreach(storage_folder) do |folder|
      next if folder == '.' || folder == '..'

      folder_path = File.join(ActiveStorage::Blob.service.root, folder)
      FileUtils.rm_rf(folder_path) if File.directory?(folder_path) && Dir.empty?(folder_path)
    end
  end
end