Skip to content

Commit

Permalink
Keep failed backorder job in dead set
Browse files Browse the repository at this point in the history
From Sidekiq's view, the job is successful when we rescue an error and
it will discard it. But we want the option to inspect the job and retry
it. Failing jobs are also reported to Bugsnag automatically.

I didn't specify `retry: false` because that discards the job as well.
But `retry: 0` should sent it straight to the dead set. No automatic
retries but it's treated like a failed job.
  • Loading branch information
mkllnk committed Sep 26, 2024
1 parent 989a6d5 commit 51b3770
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 31 deletions.
10 changes: 4 additions & 6 deletions app/jobs/backorder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class BackorderJob < ApplicationJob
}.freeze

queue_as :default
sidekiq_options retry: 0

def self.check_stock(order)
variants_needing_stock = order.variants.select do |variant|
Expand All @@ -35,15 +36,12 @@ def perform(order, linked_variants)
OrderLocker.lock_order_and_variants(order) do
place_backorder(order, linked_variants)
end
rescue StandardError => e
rescue StandardError
# If the backordering fails, we need to tell the shop owner because they
# need to organgise more stock.
Bugsnag.notify(e) do |payload|
payload.add_metadata(:order, order)
payload.add_metadata(:linked_variants, linked_variants)
end

BackorderMailer.backorder_failed(order, linked_variants).deliver_later

raise
end

def place_backorder(order, linked_variants)
Expand Down
13 changes: 5 additions & 8 deletions app/jobs/complete_backorder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# After an order cycle closed, we need to finalise open draft orders placed
# to replenish stock.
class CompleteBackorderJob < ApplicationJob
sidekiq_options retry: 0

# Required parameters:
#
# * user: to authenticate DFC requests
Expand All @@ -23,15 +25,10 @@ def perform(user, distributor, order_cycle, order_id)
adjust_quantities(user, order, urls, variants)

FdcBackorderer.new(user, urls).complete_order(order)
rescue StandardError => e
Bugsnag.notify(e) do |payload|
payload.add_metadata(:user, user)
payload.add_metadata(:distributor, distributor)
payload.add_metadata(:order_cycle, order_cycle)
payload.add_metadata(:order_id, order_id)
end

rescue StandardError
BackorderMailer.backorder_incomplete(user, distributor, order_cycle, order_id).deliver_later

raise
end

# Check if we have enough stock to reduce the backorder.
Expand Down
9 changes: 1 addition & 8 deletions spec/jobs/backorder_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,11 @@
end

describe "#peform" do
it "reports errors" do
expect(Bugsnag).to receive(:notify).and_call_original

expect {
subject.perform(nil, [])
}.not_to raise_error
end

it "notifies owner of errors" do
expect {
subject.perform(order, [])
}.to enqueue_mail(BackorderMailer, :backorder_failed)
.and raise_error(NoMethodError)
end
end

Expand Down
10 changes: 1 addition & 9 deletions spec/jobs/complete_backorder_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,10 @@
end

it "reports errors" do
expect(Bugsnag).to receive(:notify).and_call_original

expect {
subject.perform(user, distributor, order_cycle, "https://nil")
}.not_to raise_error

# Combined example for performance
expect(Bugsnag).to receive(:notify).and_call_original

expect {
subject.perform(user, distributor, order_cycle, "https://nil")
}.to enqueue_mail(BackorderMailer, :backorder_incomplete)
.and raise_error VCR::Errors::UnhandledHTTPRequestError
end
end
end

0 comments on commit 51b3770

Please sign in to comment.