-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(InvoiceError) - Add InvoiceError (#2763)
## Description When invoices fails to generate it's hard to understand what's going wrong. We're now automatically retrying invoices that are stuck in generating state. We will also create InvoiceError objects for invoices that fail to generate and finalize.
- Loading branch information
Showing
11 changed files
with
238 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
app/jobs/clock/retry_generating_subscription_invoices_job.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Clock | ||
class RetryGeneratingSubscriptionInvoicesJob < ApplicationJob | ||
include SentryCronConcern | ||
|
||
queue_as 'clock' | ||
|
||
THRESHOLD = -> { 1.day.ago } | ||
|
||
def perform | ||
Invoice.subscription.generating.where.not(id: InvoiceError.select(:id)).where('created_at < ?', THRESHOLD.call).find_each do |invoice| | ||
next unless invoice.invoice_subscriptions.any? | ||
invoicing_reasons = invoice.invoice_subscriptions.pluck(:invoicing_reason).uniq | ||
invoicing_reason = (invoicing_reasons.size == 1) ? invoicing_reasons.first : :upgrading | ||
BillSubscriptionJob.perform_later( | ||
subscriptions: invoice.subscriptions.to_a, | ||
timestamp: invoice.invoice_subscriptions.first.timestamp, | ||
invoicing_reason:, | ||
invoice:, | ||
skip_charges: invoice.skip_charges | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
class InvoiceError < ApplicationRecord | ||
# NOTE! Invoice errors will have the same id as the invoice they belong to. | ||
def self.create_for(invoice:, error:) | ||
return unless invoice | ||
|
||
create(id: invoice.id, | ||
backtrace: error.backtrace, | ||
error: error.inspect.to_json, | ||
invoice: invoice.to_json(except: :file), | ||
subscriptions: invoice.subscriptions.to_json) | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: invoice_errors | ||
# | ||
# id :uuid not null, primary key | ||
# backtrace :text | ||
# error :json | ||
# invoice :json | ||
# subscriptions :json | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateInvoiceErrors < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :invoice_errors, id: :uuid do |t| | ||
t.text :backtrace | ||
t.json :invoice | ||
t.json :subscriptions | ||
t.json :error | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
spec/jobs/clock/retry_generating_subscription_invoices_job_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe Clock::RetryGeneratingSubscriptionInvoicesJob, job: true do | ||
subject { described_class } | ||
|
||
describe '.perform' do | ||
let(:old_generating_invoice) { create(:invoice, :generating, created_at: 5.days.ago) } | ||
|
||
before do | ||
old_generating_invoice | ||
end | ||
|
||
it "does not enqueue a BillSubscriptionJob for this invoice (missing subscriptions)" do | ||
expect do | ||
described_class.perform_now | ||
end.not_to have_enqueued_job(BillSubscriptionJob) | ||
end | ||
|
||
context "with an actual invoice that should be retried" do | ||
let(:old_generating_invoice) { create(:invoice, :subscription, created_at: 5.days.ago) } | ||
|
||
before do | ||
old_generating_invoice.update(status: :generating) | ||
end | ||
|
||
it "does enqueue a BillSubscriptionJob for this invoice " do | ||
expect do | ||
described_class.perform_now | ||
end.to have_enqueued_job(BillSubscriptionJob) | ||
end | ||
|
||
context "with an existing invoice error" do | ||
let(:invoice_error) { InvoiceError.create(id: old_generating_invoice.id) } | ||
|
||
before do | ||
invoice_error | ||
end | ||
|
||
it "does not enqueue a BillSubscriptionJob for this invoice" do | ||
expect do | ||
described_class.perform_now | ||
end.not_to have_enqueued_job(BillSubscriptionJob) | ||
end | ||
end | ||
end | ||
|
||
context "with an addon" do | ||
let(:old_generating_invoice) { create(:invoice, :add_on, created_at: 5.days.ago) } | ||
|
||
before do | ||
old_generating_invoice.update(status: :generating) | ||
end | ||
|
||
it "does not enqueue a BillSubscriptionJob for this invoice" do | ||
expect do | ||
described_class.perform_now | ||
end.not_to have_enqueued_job(BillSubscriptionJob) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe InvoiceError, type: :model do | ||
let(:invoice) { create(:invoice, :generating) } | ||
let(:result) { BaseService::Result.new } | ||
let(:error) { BaseService::ValidationFailure.new(result, messages: messages) } | ||
let(:messages) { ["message1", "message2"] } | ||
|
||
let(:error_with_backtrace) do | ||
error = OpenStruct.new | ||
error.backtrace = "backtrace" | ||
error | ||
end | ||
|
||
describe ".create_for" do | ||
it "does nothing if the invoice is nil" do | ||
expect(described_class.create_for(invoice: nil, error:)).to eq(nil) | ||
end | ||
|
||
it "creates an invoice error with the same id as the invoice" do | ||
invoice_error = described_class.create_for(invoice:, error:) | ||
expect(invoice_error.id).to eq(invoice.id) | ||
end | ||
|
||
it "stores the error in the error field" do | ||
invoice_error = described_class.create_for(invoice:, error:) | ||
expect(invoice_error.error).to eq(error.inspect.to_json) | ||
end | ||
|
||
it "stores the backtrace in the backtrace field" do | ||
invoice_error = described_class.create_for(invoice:, error: error_with_backtrace) | ||
expect(invoice_error.backtrace).to eq("backtrace") | ||
end | ||
|
||
it "stores the subscriptions in the subscriptions field" do | ||
invoice_error = described_class.create_for(invoice:, error:) | ||
expect(invoice_error.subscriptions).to eq("[]") | ||
end | ||
end | ||
end |