-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] purge unattached active storage blob
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
1 parent
750f5dc
commit 5f74f68
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,4 +41,5 @@ | |
|
||
every 1.day do | ||
rake "maintenance:clear_discarded_api_actions" | ||
rake "active_storage:purge_unattached" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |