diff --git a/app/workers/virus_scan_worker.rb b/app/workers/virus_scan_worker.rb index 46268f4d..3d5df218 100644 --- a/app/workers/virus_scan_worker.rb +++ b/app/workers/virus_scan_worker.rb @@ -3,6 +3,8 @@ class VirusScanWorker include Sidekiq::Worker + sidekiq_options lock: :until_and_while_executing + def perform(asset_id) asset = Asset.find(asset_id) if asset.unscanned? diff --git a/spec/workers/virus_scan_worker_spec.rb b/spec/workers/virus_scan_worker_spec.rb index bffd9af8..6f609890 100644 --- a/spec/workers/virus_scan_worker_spec.rb +++ b/spec/workers/virus_scan_worker_spec.rb @@ -1,5 +1,6 @@ require "rails_helper" require "services" +require "sidekiq_unique_jobs/testing" RSpec.describe VirusScanWorker do let(:worker) { described_class.new } @@ -10,6 +11,15 @@ allow(Services).to receive(:virus_scanner).and_return(scanner) end + specify { expect(described_class).to have_valid_sidekiq_options } + + it "does not permit multiple jobs to be enqueued for the same asset" do + SidekiqUniqueJobs.use_config(enabled: true) do + expect { described_class.perform_in(1.minute, asset.id.to_s) }.to enqueue_sidekiq_job(described_class) + expect { described_class.perform_in(1.minute, asset.id.to_s) }.not_to enqueue_sidekiq_job(described_class) + end + end + it "calls out to the VirusScanner to scan the file" do expect(scanner).to receive(:scan).with(asset.file.path)