2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-12-22 16:03:36 +01:00
|
|
|
module DownloadManager
|
|
|
|
class ProcedureAttachmentsExport
|
|
|
|
delegate :destination, to: :@queue
|
|
|
|
|
|
|
|
attr_reader :queue
|
|
|
|
attr_accessor :errors
|
|
|
|
|
|
|
|
def initialize(procedure, attachments, destination)
|
|
|
|
@procedure = procedure
|
|
|
|
@errors = {}
|
|
|
|
@queue = ParallelDownloadQueue.new(attachments, destination)
|
2022-01-06 15:42:56 +01:00
|
|
|
@queue.on_error = proc do |attachment, path, error|
|
|
|
|
errors[path] = [attachment, path]
|
2021-12-22 16:03:36 +01:00
|
|
|
Rails.logger.error("Fail to download filename #{path} in procedure##{@procedure.id}, reason: #{error}")
|
|
|
|
end
|
2022-01-06 15:42:56 +01:00
|
|
|
end
|
2021-12-22 16:03:36 +01:00
|
|
|
|
2022-01-06 15:42:56 +01:00
|
|
|
def download_all(attempt_left: 1)
|
2021-12-22 16:03:36 +01:00
|
|
|
@queue.download_all
|
2022-01-06 15:42:56 +01:00
|
|
|
if !errors.empty? && attempt_left.positive?
|
|
|
|
retryable_queue = self.class.new(@procedure, errors.values, destination)
|
|
|
|
retryable_queue.download_all(attempt_left: 0)
|
|
|
|
retryable_queue.write_report if !retryable_queue.errors.empty?
|
|
|
|
end
|
2021-12-22 16:03:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def write_report
|
2022-10-20 09:33:26 +02:00
|
|
|
manifest_path = File.join(destination, '-LISTE-DES-FICHIERS-EN-ERREURS.txt')
|
2021-12-22 16:03:36 +01:00
|
|
|
manifest_content = errors.map do |file_basename, _failed|
|
|
|
|
"Impossible de récupérer le fichier #{file_basename}"
|
|
|
|
end
|
|
|
|
.join("\n")
|
|
|
|
File.write(manifest_path, manifest_content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|