Skip to content

Commit

Permalink
[feature] purge unattached active storage blob
Browse files Browse the repository at this point in the history
Addresses: #1083

DEMO:


https://user-images.githubusercontent.com/50227291/219999900-c683255b-018c-4208-8b33-70b541494815.mov

Co-authored-by: Pralish Kayastha <50227291+Pralish@users.noreply.github.com>
  • Loading branch information
donrestarone and Pralish authored Feb 22, 2023
1 parent 750f5dc commit 5f74f68
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@

every 1.day do
rake "maintenance:clear_discarded_api_actions"
rake "active_storage:purge_unattached"
end
13 changes: 13 additions & 0 deletions lib/tasks/active_storage.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# https://github.com/rails/rails/blob/main/guides/source/active_storage_overview.md#purging-unattached-uploads
namespace :active_storage do
desc "Purges unattached Active Storage blobs."

task purge_unattached: :environment do
Subdomain.all_with_public_schema.each do |subdomain|
Apartment::Tenant.switch subdomain.name do
p "** Purging unattached Active Storage blobs for #{subdomain.name}** @ #{Time.now}"
ActiveStorage::Blob.unattached.where('active_storage_blobs.created_at <= ?', 1.day.ago).find_each(&:purge_later)
end
end
end
end
48 changes: 48 additions & 0 deletions test/tasks/purge_unattached_task_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'test_helper'
require 'rake'

class PurgeUnattachedTaskTest < ActiveSupport::TestCase
include Rake::DSL

def setup
Rake.application.rake_require('tasks/active_storage')
Rake::Task.define_task(:environment)
Sidekiq::Testing.fake!
end

test "purges only unattached Active Storage blobs that are older than 1 day" do
attached_blob = create_attached_blob
unattached_blob = create_unattached_blob

# Wait for the unattached blob to be older than 1 day
travel 2.days

another_unattached_blob = create_unattached_blob

perform_enqueued_jobs do
Rake::Task['active_storage:purge_unattached'].invoke
Sidekiq::Worker.drain_all
end

# purges unattached blob older than 1 day
assert_not ActiveStorage::Blob.exists?(unattached_blob.id)

# does not purge attached blob
assert ActiveStorage::Blob.exists?(attached_blob.id)

# does not purge unattached blob newer than 1 day
assert ActiveStorage::Blob.exists?(another_unattached_blob.id)
end

private

def create_attached_blob
user = users(:public)
user.avatar.attach(io: File.open(Rails.root.join('test', 'fixtures', 'files', 'fixture_image.png')), filename: 'fixture_image.png', content_type: 'image/png')
user.avatar.blob
end

def create_unattached_blob
ActiveStorage::Blob.create_after_upload!(io: StringIO.new('unattached blob data'), filename: 'unattached.txt', content_type: 'text/plain')
end
end

0 comments on commit 5f74f68

Please sign in to comment.