2022-09-27 01:08:19 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Vacuum::MediaAttachmentsVacuum
|
|
|
|
TTL = 1.day.freeze
|
|
|
|
|
|
|
|
def initialize(retention_period)
|
|
|
|
@retention_period = retention_period
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
vacuum_orphaned_records!
|
2022-10-26 10:10:48 +00:00
|
|
|
vacuum_cached_files! if retention_period?
|
2022-09-27 01:08:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def vacuum_cached_files!
|
2023-06-26 12:17:41 +00:00
|
|
|
media_attachments_past_retention_period.find_in_batches do |media_attachments|
|
|
|
|
AttachmentBatch.new(MediaAttachment, media_attachments).clear
|
2022-09-27 01:08:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def vacuum_orphaned_records!
|
2023-06-26 12:17:41 +00:00
|
|
|
orphaned_media_attachments.find_in_batches do |media_attachments|
|
|
|
|
AttachmentBatch.new(MediaAttachment, media_attachments).delete
|
|
|
|
end
|
2022-09-27 01:08:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def media_attachments_past_retention_period
|
2024-01-24 10:32:54 +00:00
|
|
|
MediaAttachment
|
|
|
|
.remote
|
|
|
|
.cached
|
|
|
|
.created_before(@retention_period.ago)
|
|
|
|
.updated_before(@retention_period.ago)
|
2022-09-27 01:08:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def orphaned_media_attachments
|
2024-01-24 10:32:54 +00:00
|
|
|
MediaAttachment
|
|
|
|
.unattached
|
|
|
|
.created_before(TTL.ago)
|
2022-09-27 01:08:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def retention_period?
|
|
|
|
@retention_period.present?
|
|
|
|
end
|
|
|
|
end
|