demarches-normaliennes/app/jobs/cleanup_stale_exports_job.rb
2019-10-24 16:27:10 +02:00

26 lines
914 B
Ruby
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class CleanupStaleExportsJob < ApplicationJob
queue_as :cron
def perform(*args)
attachments = ActiveStorage::Attachment.where(
"name in ('csv_export_file', 'ods_export_file', 'xlsx_export_file') and created_at < ?",
Procedure::MAX_DUREE_CONSERVATION_EXPORT.ago
)
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