2022-09-26 21:08:19 -04: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 06:10:48 -04:00
|
|
|
vacuum_cached_files! if retention_period?
|
2022-09-26 21:08:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def vacuum_cached_files!
|
2023-06-26 08:17:41 -04:00
|
|
|
media_attachments_past_retention_period.find_in_batches do |media_attachments|
|
|
|
|
AttachmentBatch.new(MediaAttachment, media_attachments).clear
|
2022-09-26 21:08:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def vacuum_orphaned_records!
|
2023-06-26 08:17:41 -04:00
|
|
|
orphaned_media_attachments.find_in_batches do |media_attachments|
|
|
|
|
AttachmentBatch.new(MediaAttachment, media_attachments).delete
|
|
|
|
end
|
2022-09-26 21:08:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def media_attachments_past_retention_period
|
2023-11-30 08:30:35 -05: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-26 21:08:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def orphaned_media_attachments
|
2023-11-30 08:30:35 -05:00
|
|
|
MediaAttachment.unattached.where(MediaAttachment.arel_table[:created_at].lt(TTL.ago))
|
2022-09-26 21:08:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def retention_period?
|
|
|
|
@retention_period.present?
|
|
|
|
end
|
|
|
|
end
|