cleanup the queue flag during cleanup job

This commit is contained in:
clemkeirua 2019-10-24 15:30:09 +02:00 committed by simon lehericey
parent da590cc73a
commit cb5f922bb6

View file

@ -2,9 +2,25 @@ class CleanupStaleExportsJob < ApplicationJob
queue_as :cron
def perform(*args)
ActiveStorage::Attachment.where(
attachments = ActiveStorage::Attachment.where(
"name in ('csv_export_file', 'ods_export_file', 'xlsx_export_file') and created_at < ?",
Procedure::MAX_DUREE_CONSERVATION_EXPORT.ago
).find_each(&:purge_later)
)
attachments.each do |attachment|
procedure = Procedure.find(attachment.record_id)
# export can't be queued if it's already attached
#  so we clean the flag up just in case it was not removed during
# the asynchronous generation
case attachment.name
when 'csv_export_file'
procedure.update(csv_export_queued: false)
when 'ods_export_file'
procedure.update(ods_export_queued: false)
when 'xlsx_export_file'
procedure.update(xlsx_export_queued: false)
end
# and we remove the stale attachment
attachment.purge_later
end
end
end