Skip to content

Commit

Permalink
Rename VirusScanWorker
Browse files Browse the repository at this point in the history
`Sidekiq::Worker` has been deprecated in Sidekiq 7, so we need to
replace it with `Sidekiq::Job`, and then rename the workers to be jobs.
  • Loading branch information
brucebolt committed Sep 24, 2024
1 parent 6ef5bf3 commit bdea4dc
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/models/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def reset_state
end

def schedule_virus_scan
VirusScanWorker.perform_async(id.to_s) if unscanned? && redirect_url.blank?
VirusScanJob.perform_async(id.to_s) if unscanned? && redirect_url.blank?
end

def file_exists?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require "services"

class VirusScanWorker
include Sidekiq::Worker
class VirusScanJob
include Sidekiq::Job

sidekiq_options lock: :until_and_while_executing

def perform(asset_id)
asset = Asset.find(asset_id)
if asset.unscanned?
begin
Rails.logger.info("#{asset_id} - VirusScanWorker#perform - Virus scan started")
Rails.logger.info("#{asset_id} - VirusScanJob#perform - Virus scan started")
Services.virus_scanner.scan(asset.file.path)
asset.scanned_clean!
rescue VirusScanner::InfectedFile => e
Expand Down
10 changes: 5 additions & 5 deletions spec/models/asset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@
it "schedules a scan after create" do
a = described_class.new(file: load_fixture_file("asset.png"))

expect(VirusScanWorker).to receive(:perform_async).with(a.id)
expect(VirusScanJob).to receive(:perform_async).with(a.id)

a.save!
end
Expand All @@ -465,7 +465,7 @@
a = FactoryBot.create(:clean_asset)
a.file = load_fixture_file("lorem.txt")

expect(VirusScanWorker).to receive(:perform_async).with(a.id)
expect(VirusScanJob).to receive(:perform_async).with(a.id)

a.save!
end
Expand All @@ -475,7 +475,7 @@
original_filename = a.file.send(:original_filename)
a.file = load_fixture_file("lorem.txt", named: original_filename)

expect(VirusScanWorker).to receive(:perform_async).with(a.id)
expect(VirusScanJob).to receive(:perform_async).with(a.id)

a.save!
end
Expand All @@ -484,15 +484,15 @@
a = FactoryBot.create(:clean_asset)
a.created_at = 5.days.ago

expect(VirusScanWorker).not_to receive(:perform_async)
expect(VirusScanJob).not_to receive(:perform_async)

a.save!
end

it "does not schedule a scan if a redirect url is present" do
a = FactoryBot.create(:asset, redirect_url: "/some-redirect")

expect(VirusScanWorker).not_to receive(:perform_async)
expect(VirusScanJob).not_to receive(:perform_async)

a.save!
end
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/asset_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
it "does not result in an invalid transition error when a redirect is received in short succession after a create" do
# use threads to simulate multiple sidekiq workers
threads = []
allow(VirusScanWorker).to receive(:perform_async) do |asset_id|
allow(VirusScanJob).to receive(:perform_async) do |asset_id|
threads << Thread.new do
sleep(0.5)
asset = Asset.find(asset_id)
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/virus_scanning_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
expect(response).to have_http_status(:not_found)

allow(Services.virus_scanner).to receive(:scan)
VirusScanWorker.drain
VirusScanJob.drain

get download_media_path(id: asset, filename: "lorem.txt")
expect(response).to have_http_status(:not_found)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "services"
require "sidekiq_unique_jobs/testing"

RSpec.describe VirusScanWorker do
RSpec.describe VirusScanJob do
let(:worker) { described_class.new }
let(:asset) { FactoryBot.create(:asset) }
let(:scanner) { instance_double(VirusScanner) }
Expand Down

0 comments on commit bdea4dc

Please sign in to comment.