demarches-normaliennes/app/jobs/virus_scanner_job.rb
Pierre de La Morinerie 292dc84eb4 jobs: ignore deleted file in Virus Scan
We have errors in production where the job starts correctly (i.e. the
blob exists), but `blob.open` fails with a `ActiveStorage::FileNotFound`
error.

When checking later in production, the blob has been deleted.

This points to the blob (and the file) being deleted during the virus
scan job.

In that case, ignore the error (rather than retrying the job).
2020-09-03 11:00:59 +02:00

17 lines
551 B
Ruby

class VirusScannerJob < ApplicationJob
queue_as :active_storage_analysis
# If by the time the job runs the blob has been deleted, ignore the error
discard_on ActiveRecord::RecordNotFound
# If the file is deleted during the scan, ignore the error
discard_on ActiveStorage::FileNotFoundError
def perform(blob)
metadata = extract_metadata_via_virus_scanner(blob)
blob.update!(metadata: blob.metadata.merge(metadata))
end
def extract_metadata_via_virus_scanner(blob)
ActiveStorage::VirusScanner.new(blob).metadata
end
end