diff --git a/app/jobs/invoices/refresh_draft_job.rb b/app/jobs/invoices/refresh_draft_job.rb index c38659fadcc..c22fdb6a1bf 100644 --- a/app/jobs/invoices/refresh_draft_job.rb +++ b/app/jobs/invoices/refresh_draft_job.rb @@ -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 diff --git a/spec/jobs/invoices/refresh_draft_job_spec.rb b/spec/jobs/invoices/refresh_draft_job_spec.rb index aa7657a363e..0c0b231ba21 100644 --- a/spec/jobs/invoices/refresh_draft_job_spec.rb +++ b/spec/jobs/invoices/refresh_draft_job_spec.rb @@ -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 @@ -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) }