This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2022-09-26 18:08:19 -07: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 03:10:48 -07:00
|
|
|
vacuum_cached_files! if retention_period?
|
2022-09-26 18:08:19 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def vacuum_cached_files!
|
2023-06-26 05:17:41 -07:00
|
|
|
media_attachments_past_retention_period.find_in_batches do |media_attachments|
|
|
|
|
AttachmentBatch.new(MediaAttachment, media_attachments).clear
|
2022-09-26 18:08:19 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def vacuum_orphaned_records!
|
2023-06-26 05:17:41 -07:00
|
|
|
orphaned_media_attachments.find_in_batches do |media_attachments|
|
|
|
|
AttachmentBatch.new(MediaAttachment, media_attachments).delete
|
|
|
|
end
|
2022-09-26 18:08:19 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def media_attachments_past_retention_period
|
|
|
|
MediaAttachment.unscoped.remote.cached.where(MediaAttachment.arel_table[:created_at].lt(@retention_period.ago)).where(MediaAttachment.arel_table[:updated_at].lt(@retention_period.ago))
|
|
|
|
end
|
|
|
|
|
|
|
|
def orphaned_media_attachments
|
|
|
|
MediaAttachment.unscoped.unattached.where(MediaAttachment.arel_table[:created_at].lt(TTL.ago))
|
|
|
|
end
|
|
|
|
|
|
|
|
def retention_period?
|
|
|
|
@retention_period.present?
|
|
|
|
end
|
|
|
|
end
|