Skip to content
Draft
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
8 changes: 5 additions & 3 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ def update
end

def destroy
@provider.destroy!

respond_to do |format|
format.html { redirect_to providers_path, status: :see_other, notice: "Provider was successfully destroyed." }
if @provider.destroy
format.html { redirect_to providers_path, status: :see_other, notice: "Provider was successfully destroyed." }
else
format.html { redirect_to providers_path, status: :see_other, alert: @provider.errors.full_messages.to_sentence }
end
end
end

Expand Down
10 changes: 2 additions & 8 deletions app/jobs/file_upload_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ class FileUploadJob < ApplicationJob
# or use a more specific key to avoid blocking all jobs for a language
limits_concurrency to: 3, key: ->(_language_id, content_id, _content_type) { "hard-limit" }

retry_on AzureFileShares::Errors::ApiError, wait: :exponentially_longer, attempts: 3
retry_on Timeout::Error, wait: :exponentially_longer, attempts: 2

discard_on StandardError do |job, error|
Rails.logger.error "FileUploadJob failed permanently: #{error.message}"
Rails.logger.error "Job arguments: #{job.arguments}"
Rails.logger.error "Suggestion: Check provider names for invalid characters if Azure API errors"
end
retry_on AzureFileShares::Errors::ApiError, wait: :polynomially_longer, attempts: 3
retry_on Timeout::Error, wait: :polynomially_longer, attempts: 2

def perform(language_id, content_id, content_type, share = ENV["AZURE_STORAGE_SHARE_NAME"])
@language = Language.find(language_id)
Expand Down
2 changes: 1 addition & 1 deletion app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Provider < ApplicationRecord
has_many :regions, through: :branches
has_many :contributors
has_many :users, through: :contributors
has_many :topics
has_many :topics, dependent: :restrict_with_error

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we need to keep topics without provider?

has_many :beacon_providers, dependent: :destroy
has_many :beacons, through: :beacon_providers

Expand Down
17 changes: 17 additions & 0 deletions spec/jobs/file_upload_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@
end
end

context "when generating the file fails" do
before { allow(CsvGenerator::Topics).to receive(:new).and_raise(NoMethodError, "undefined method 'name' for nil") }

it "raises so the job is recorded as failed" do
expect { described_class.perform_now(language.id, "topics", "file") }.to raise_error(NoMethodError)
end
end

context "when Azure returns an API error" do
before { allow(FileWorker).to receive(:new).and_raise(AzureFileShares::Errors::ApiError.new("ServerBusy")) }

it "retries the job" do
expect { described_class.perform_now(language.id, "topics", "file") }
.to have_enqueued_job(described_class).with(language.id, "topics", "file")
end
end

context "when provider specific file" do
let(:provider) { create(:provider, name: "Test Provider") }

Expand Down
14 changes: 14 additions & 0 deletions spec/requests/providers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,19 @@

expect(response).to redirect_to(providers_url)
end

context "when the provider has topics" do
it "keeps the provider and explains why" do
provider = Provider.create! valid_attributes
create(:topic, provider:)

expect {
delete provider_url(provider)
}.not_to change(Provider, :count)

expect(response).to redirect_to(providers_url)
expect(flash[:alert]).to match(/topics/i)
end
end
end
end
Loading