Skip to content

Commit

Permalink
Don't execute RefreshDraft service when the ready_to_be_refreshed is …
Browse files Browse the repository at this point in the history
…not true
  • Loading branch information
nudded committed Oct 3, 2024
1 parent 037290f commit 05dddc2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/jobs/invoices/refresh_draft_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ module Invoices
class RefreshDraftJob < ApplicationJob
queue_as 'invoices'

unique :until_executed, on_conflict: :log
unique :until_executed, on_conflict: :log, lock_ttl: 6.hours

def perform(invoice)
# if this has already been set to false, we can skip the job
return unless invoice.ready_to_be_refreshed?

::Invoices::RefreshDraftService.call(invoice:)
end
end
Expand Down
20 changes: 19 additions & 1 deletion spec/jobs/invoices/refresh_draft_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

RSpec.describe Invoices::RefreshDraftJob, type: :job do
let(:invoice) { create(:invoice) }
let(:invoice) { create(:invoice, ready_to_be_refreshed: true) }
let(:result) { BaseService::Result.new }

let(:refresh_service) do
Expand All @@ -20,6 +20,24 @@
expect(refresh_service).to have_received(:call)
end

it 'does not delegate to the RefreshDraft service if the ready_to_be_refreshed? is false' do
allow(Invoices::RefreshDraftService).to receive(:new).with(invoice:).and_return(refresh_service)
allow(refresh_service).to receive(:call)

invoice.update ready_to_be_refreshed: false
described_class.perform_now(invoice)

expect(Invoices::RefreshDraftService).not_to have_received(:new)
expect(refresh_service).not_to have_received(:call)
end

it 'has a lock_ttl of 6.hours' do
# When there's lots of draft invoices to be refreshed, we might end up enqueueing multiple of them.
# This will block all queues with lower prio than the `invoices` queue. (e.g. wallets). This is undesirable,
# so we bump the lock_ttl for this job to 6 hours
expect(described_class.new.lock_options[:lock_ttl]).to eq(6.hours)
end

context 'when there was a tax fetching error in RefreshDraft service' do
let(:integration_customer) { create(:anrok_customer, customer: invoice.customer) }
let(:response) { instance_double(Net::HTTPOK) }
Expand Down

0 comments on commit 05dddc2

Please sign in to comment.