75a1046315
Follow-up of #5953. Refactor the concerns with two goals: - Getting closer from the way ActiveStorage adds its own hooks. Usually ActiveStorage does this using an `Attachment#after_create` hook, which then delegates to the blob to enqueue the job. - Enqueuing each job only once. By hooking on `Attachment#after_create`, we guarantee each job will be added only once. We then let the jobs themselves check if they are relevant or not, and retry or discard themselves if necessary. We also need to update the tests a bit, because Rails' `perform_enqueued_jobs(&block)` test helper doesn't honor the `retry_on` clause of jobs. Instead it forwards the exception to the caller – which makes the test fail. Instead we use the inline version of `perform_enqueued_jobs()`, without a block, which properly ignores errors catched by retry_on.
27 lines
1,014 B
Ruby
27 lines
1,014 B
Ruby
class VirusScannerJob < ApplicationJob
|
|
class FileNotAnalyzedYetError < StandardError
|
|
end
|
|
|
|
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
|
|
# If the file is not analyzed yet, retry later (to avoid clobbering metadata)
|
|
retry_on FileNotAnalyzedYetError, wait: :exponentially_longer, attempts: 10
|
|
# If for some reason the file appears invalid, retry for a while
|
|
retry_on ActiveStorage::IntegrityError, attempts: 10, wait: 5.seconds
|
|
|
|
def perform(blob)
|
|
if !blob.analyzed? then raise FileNotAnalyzedYetError end
|
|
if blob.virus_scanner.done? then return end
|
|
|
|
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
|