Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to avoid some of the Enoent issues #1258

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Asset

mount_uploader :file, AssetUploader

before_save :store_metadata, unless: :uploaded?
before_save :store_metadata, unless: :uploaded?, if: -> { changes.include?(:file) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might help reduce the number of raised errors.

It wouldn't if the second process is already inside the store_metadata method

after_save :schedule_virus_scan
after_save :update_indirect_replacements_on_publish
after_save :backpropagate_replacement
Expand Down
7 changes: 6 additions & 1 deletion app/workers/virus_scan_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ def perform(asset_id)
begin
Rails.logger.info("#{asset_id} - VirusScanWorker#perform - Virus scan started")
Services.virus_scanner.scan(asset.file.path)
asset.scanned_clean!
# This is to deal with a concurrency issue in production
# It looks like sometimes VirusScannerWorker can be scheduled several times for one file
# And the other process might change the state already of as the virus scan take time
# This if condition is to avoid unnecessary state transitions and therefore other
# concurrency issues
asset.scanned_clean! if Asset.find(asset_id).unscanned?
rescue VirusScanner::InfectedFile => e
GovukError.notify(e, extra: { id: asset.id, filename: asset.filename })
asset.scanned_infected!
Expand Down
12 changes: 12 additions & 0 deletions spec/workers/virus_scan_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
end
end

context "when the asset becomes marked clean by another process" do
it "does change the asset state" do
allow(scanner).to receive(:scan) do
asset.scanned_infected!
end

worker.perform(asset.id)

expect(asset.reload).not_to be_clean
end
end

context "when the asset is already marked as infected" do
let(:asset) { FactoryBot.create(:infected_asset) }

Expand Down