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
|
2023-11-30 14:30:35 +01:00
|
|
|
MediaAttachment.remote.cached.where(MediaAttachment.arel_table[:created_at].lt(@retention_period.ago)).where(MediaAttachment.arel_table[:updated_at].lt(@retention_period.ago))
|
2022-09-27 03:08:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def orphaned_media_attachments
|
2023-11-30 14:30:35 +01:00
|
|
|
MediaAttachment.unattached.where(MediaAttachment.arel_table[:created_at].lt(TTL.ago))
|
2022-09-27 03:08:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def retention_period?
|
|
|
|
@retention_period.present?
|
|
|
|
end
|
|
|
|
end
|