Class: DestroyDocumentDistributionsJob
- Inherits:
-
ApplicationJob
- Object
- ApplicationJob
- DestroyDocumentDistributionsJob
- Defined in:
- app/jobs/destroy_document_distributions_job.rb
Overview
DestroyDocumentDistributionsJob
This job processes a CSV file containing document distribution records to be destroyed. It handles large CSV files in the background to prevent timeouts and provides progress updates via notifications.
Instance Method Summary collapse
Instance Method Details
#perform(file_path, current_user) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/jobs/destroy_document_distributions_job.rb', line 13 def perform(file_path, current_user) logger.debug("DestroyDocumentDistributionsJob: Starting destruction of distributions from #{file_path}") destroyed_count = 0 error_count = 0 errors = [] begin unless File.exist?(file_path) raise ArgumentError, "File not found: #{file_path}" end CSV.foreach(file_path, headers: true) do |row| logger.debug("Processing CSV Row: #{row.to_hash}") begin reference_type = ReferenceType.find_by(name: row.to_hash["reference_type"]) if reference_type.nil? error_count += 1 errors << "Reference type not found: #{row.to_hash["reference_type"]}" next end destroyed = DocumentDistribution.destroy_by( friendlier_id: row.to_hash["friendlier_id"], reference_type_id: reference_type.id, url: row.to_hash["distribution_url"] ) if destroyed.any? destroyed_count += destroyed.count logger.debug("Destroyed: #{row.to_hash}") else logger.debug("Not found to destroy: #{row.to_hash}") end rescue => e error_count += 1 errors << "Error processing row #{row.to_hash}: #{e.message}" logger.error("Error destroying distribution: #{e}") end end # Create success notification = "DISTRIBUTIONS DESTROYED|#{ActionController::Base.helpers.number_with_delimiter(destroyed_count)} distributions destroyed" if error_count > 0 += " (#{error_count} errors)" end notification = ExportNotification.with(message: ) notification.deliver(current_user) # Clean up the temporary file File.delete(file_path) if File.exist?(file_path) logger.debug("DestroyDocumentDistributionsJob: Completed. Destroyed #{destroyed_count} distributions with #{error_count} errors") rescue => e logger.error("DestroyDocumentDistributionsJob: Fatal error - #{e}") # Create error notification notification = ExportNotification.with(message: "DISTRIBUTION DESTROY FAILED|Error: #{e.message}") notification.deliver(current_user) # Clean up the temporary file File.delete(file_path) if File.exist?(file_path) end end |