-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(Docker): Add minio service * feat(rake): Add new tasks to cleanup s3 bucket * fix: Add S3 purge rake task * fix: S3 Bucket endpoint for docker local * fix(rake): Active storage clear orphans job * fix(sidekiq): Add sidekiq configuration * fix: Logger for active_storage.rake job * fix: Prevent duplicated ActiveRecord Query
- Loading branch information
1 parent
62f239b
commit d62d8a7
Showing
5 changed files
with
112 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActiveStorageClearOrphansJob < ApplicationJob | ||
include ActionView::Helpers::NumberHelper | ||
queue_as :default | ||
|
||
def perform(**args) | ||
limit = args[:limit] || 10_000 | ||
Rails.logger.info "Looking for orphan blobs in S3... (limit: #{limit})" | ||
objects = ActiveStorage::Blob.service.bucket.objects | ||
Rails.logger.info "Total files: #{objects.size}" | ||
|
||
current_iteration = 0 | ||
sum = 0 | ||
orphans_count = 0 | ||
objects.each do |obj| | ||
break if current_iteration >= limit | ||
|
||
current_iteration += 1 | ||
next if ActiveStorage::Blob.exists?(key: obj.key) | ||
|
||
sum += delete_object(obj) | ||
orphans_count += 1 | ||
end | ||
|
||
Rails.logger.info "Size: #{number_to_human_size(sum)} in #{orphans_count} files" | ||
Rails.logger.info "Configuration limit is #{limit} files" | ||
Rails.logger.info "Terminated task... " | ||
end | ||
|
||
private | ||
|
||
def delete_object(obj) | ||
Rails.logger.info "Removing orphan: #{obj.key}" | ||
size = obj.size | ||
obj.delete | ||
size | ||
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
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
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
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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :active_storage do | ||
namespace :purge do | ||
desc "Purge orphan blobs in databa" | ||
task blobs: :environment do | ||
Rails.logger.info "Looking for blobs without attachments in database..." | ||
blobs = ActiveStorage::Blob.where.not(id: ActiveStorage::Attachment.select(:blob_id)) | ||
|
||
if blobs.count.zero? | ||
Rails.logger.info "Database is clean !" | ||
Rails.logger.info "Terminating task..." | ||
else | ||
Rails.logger.info "Found #{blobs.count} orphan blobs !" | ||
blobs.each(&:purge) | ||
Rails.logger.info "Task terminated !" | ||
end | ||
end | ||
|
||
desc "Purge orphan blobs in S3" | ||
task s3: :environment do | ||
limit = ENV.fetch("S3_LIMIT", "10000").to_i | ||
ActiveStorageClearOrphansJob.perform_later(limit: limit) | ||
end | ||
end | ||
end |