2022-09-27 03:08:19 +02: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 12:10:48 +02:00
|
|
|
vacuum_cached_files! if retention_period?
|
2022-09-27 03:08:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def vacuum_cached_files!
|
2023-06-26 14:17:41 +02:00
|
|
|
media_attachments_past_retention_period.find_in_batches do |media_attachments|
|
|
|
|
AttachmentBatch.new(MediaAttachment, media_attachments).clear
|
2022-09-27 03:08:19 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def vacuum_orphaned_records!
|
2023-06-26 14:17:41 +02:00
|
|
|
orphaned_media_attachments.find_in_batches do |media_attachments|
|
|
|
|
AttachmentBatch.new(MediaAttachment, media_attachments).delete
|
|
|
|
end
|
2022-09-27 03:08:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def media_attachments_past_retention_period
|
2024-01-24 11:32:54 +01:00
|
|
|
MediaAttachment
|
|
|
|
.remote
|
|
|
|
.cached
|
|
|
|
.created_before(@retention_period.ago)
|
|
|
|
.updated_before(@retention_period.ago)
|
2022-09-27 03:08:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def orphaned_media_attachments
|
2024-01-24 11:32:54 +01:00
|
|
|
MediaAttachment
|
|
|
|
.unattached
|
|
|
|
.created_before(TTL.ago)
|
2022-09-27 03:08:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def retention_period?
|
|
|
|
@retention_period.present?
|
|
|
|
end
|
|
|
|
end
|