Skip to content

Commit

Permalink
fix(integration): Add more auto retries for reuqest limit errors (#2570)
Browse files Browse the repository at this point in the history
## Context

Many dead jobs are raised by integration with an
`SSS_REQUEST_LIMIT_EXCEEDED` error code.
Theses jobs should be automatically retried without ending up in the
dead queue.

## Description

This PR adds the logic to dig in the error message of integration
response looking for the error code. When this error code is
`SSS_REQUEST_LIMIT_EXCEEDED` it now raises a new `RequestLimitError` and
related jobs are now able to reprocess up to 10 times
  • Loading branch information
vincent-pochet authored Sep 12, 2024
1 parent 100b231 commit 36110b5
Show file tree
Hide file tree
Showing 20 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class CreateJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(credit_note:)
result = Integrations::Aggregator::CreditNotes::CreateService.call(credit_note:)
Expand Down
1 change: 1 addition & 0 deletions app/jobs/integrations/aggregator/fetch_items_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class FetchItemsJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(integration:)
result = Integrations::Aggregator::ItemsService.call(integration:)
Expand Down
1 change: 1 addition & 0 deletions app/jobs/integrations/aggregator/fetch_tax_items_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class FetchTaxItemsJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(integration:)
result = Integrations::Aggregator::TaxItemsService.call(integration:)
Expand Down
1 change: 1 addition & 0 deletions app/jobs/integrations/aggregator/invoices/create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class CreateJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(invoice:)
result = Integrations::Aggregator::Invoices::CreateService.call(invoice:)
Expand Down
1 change: 1 addition & 0 deletions app/jobs/integrations/aggregator/payments/create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class CreateJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 5
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(payment:)
result = Integrations::Aggregator::Payments::CreateService.call(payment:)
Expand Down
1 change: 1 addition & 0 deletions app/jobs/integrations/aggregator/perform_sync_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class PerformSyncJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(integration:, sync_tax_items: false)
sync_result = Integrations::Aggregator::SyncService.call(integration:)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class CreateJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(invoice:)
result = Integrations::Aggregator::SalesOrders::CreateService.call(invoice:)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class SendRestletEndpointJob < ApplicationJob
queue_as 'integrations'

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 10

def perform(integration:)
result = Integrations::Aggregator::SendRestletEndpointService.call(integration:)
Expand Down
9 changes: 9 additions & 0 deletions app/services/integrations/aggregator/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Integrations
module Aggregator
class BaseService < BaseService
BASE_URL = 'https://api.nango.dev/'
REQUEST_LIMIT_ERROR_CODE = 'SSS_REQUEST_LIMIT_EXCEEDED'

def initialize(integration:, options: {})
@integration = integration
Expand Down Expand Up @@ -101,6 +102,14 @@ def message(error)
json.dig('error', 'payload', 'message').presence ||
json.dig('error', 'message')
end

def request_limit_error?(http_error)
return false unless http_error.error_code.to_i == 500

http_error.json_message.dig('error', 'payload', 'error', 'code') == REQUEST_LIMIT_ERROR_CODE
rescue JSON::ParserError
false
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def call

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)

code = code(e)
message = message(e)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def call

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)

code = code(e)
message = message(e)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def call

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)

code = code(e)
message = message(e)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def call

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)

code = code(e)
message = message(e)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def call

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)

code = code(e)
message = message(e)

Expand Down
14 changes: 14 additions & 0 deletions app/services/integrations/aggregator/request_limit_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Integrations
module Aggregator
class RequestLimitError < StandardError
def initialize(http_error)
@http_error = http_error
super(http_error.message)
end

attr_reader :http_error
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def call

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)

code = code(e)
message = message(e)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def call

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)

code = code(e)
message = message(e)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def call

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)

code = code(e)
message = message(e)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def call

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)

code = code(e)
message = message(e)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def call

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)

code = code(e)
message = message(e)

Expand Down

0 comments on commit 36110b5

Please sign in to comment.