-
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.
Merge branch 'develop' into fix/custom_sort_for_processes_missing_date
- Loading branch information
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 |