Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(unique-jobs): Fix on retry in unique jobs #2862

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/jobs/integrations/aggregator/credit_notes/create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module CreditNotes
class CreateJob < ApplicationJob
queue_as 'integrations'

unique :until_executed, on_conflict: :log
# https://github.com/veeqo/activejob-uniqueness/issues/75
# retry_on does not work with until_executed strategy
unique :until_executed_patch, on_conflict: :log

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100
Expand Down
4 changes: 3 additions & 1 deletion app/jobs/integrations/aggregator/invoices/create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module Invoices
class CreateJob < ApplicationJob
queue_as 'integrations'

unique :until_executed, on_conflict: :log
# https://github.com/veeqo/activejob-uniqueness/issues/75
# retry_on does not work with until_executed strategy
unique :until_executed_patch, on_conflict: :log

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 3
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module Crm
class CreateCustomerAssociationJob < ApplicationJob
queue_as 'integrations'

unique :until_executed, on_conflict: :log

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

Expand Down
2 changes: 0 additions & 2 deletions app/jobs/integrations/aggregator/invoices/crm/create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module Crm
class CreateJob < ApplicationJob
queue_as 'integrations'

unique :until_executed, on_conflict: :log

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 10
retry_on Integrations::Aggregator::BasePayload::Failure, wait: :polynomially_longer, attempts: 10
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100
Expand Down
2 changes: 0 additions & 2 deletions app/jobs/integrations/aggregator/invoices/crm/update_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module Crm
class UpdateJob < ApplicationJob
queue_as 'integrations'

unique :until_executed, on_conflict: :log

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 10
retry_on Integrations::Aggregator::BasePayload::Failure, wait: :polynomially_longer, attempts: 10
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100
Expand Down
4 changes: 3 additions & 1 deletion app/jobs/integrations/aggregator/payments/create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module Payments
class CreateJob < ApplicationJob
queue_as 'integrations'

unique :until_executed, on_conflict: :log
# https://github.com/veeqo/activejob-uniqueness/issues/75
# retry_on does not work with until_executed strategy
unique :until_executed_patch, on_conflict: :log

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 5
retry_on Integrations::Aggregator::BasePayload::Failure, wait: :polynomially_longer, attempts: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module Crm
class CreateCustomerAssociationJob < ApplicationJob
queue_as 'integrations'

unique :until_executed, on_conflict: :log

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module Crm
class CreateJob < ApplicationJob
queue_as 'integrations'

unique :until_executed, on_conflict: :log

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 10
retry_on Integrations::Aggregator::BasePayload::Failure, wait: :polynomially_longer, attempts: 10
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module Crm
class UpdateJob < ApplicationJob
queue_as 'integrations'

unique :until_executed, on_conflict: :log

retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 10
retry_on Integrations::Aggregator::BasePayload::Failure, wait: :polynomially_longer, attempts: 10
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/invoices/create_pay_in_advance_charge_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CreatePayInAdvanceChargeJob < ApplicationJob

retry_on Sequenced::SequenceError

unique :until_executed, on_conflict: :log
unique :until_executed_patch, on_conflict: :log

def perform(charge:, event:, timestamp:, invoice: nil)
result = Invoices::CreatePayInAdvanceChargeService.call(charge:, event:, timestamp:, invoice:)
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/invoices/payments/adyen_create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Payments
class AdyenCreateJob < ApplicationJob
queue_as 'providers'

unique :until_executed
unique :until_executed_patch

retry_on Faraday::ConnectionFailed, wait: :polynomially_longer, attempts: 6

Expand Down
2 changes: 1 addition & 1 deletion app/jobs/invoices/payments/stripe_create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Payments
class StripeCreateJob < ApplicationJob
queue_as 'providers'

unique :until_executed, on_conflict: :log
unique :until_executed_patch, on_conflict: :log

retry_on ::Stripe::RateLimitError, wait: :polynomially_longer, attempts: 6
retry_on ::Stripe::APIConnectionError, wait: :polynomially_longer, attempts: 6
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/invoices/prepaid_credit_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class PrepaidCreditJob < ApplicationJob
queue_as 'wallets'

retry_on ActiveRecord::StaleObjectError, wait: :polynomially_longer, attempts: 6
unique :until_executed, on_conflict: :log
unique :until_executed_patch, on_conflict: :log

def perform(invoice)
wallet_transaction = invoice.fees.find_by(fee_type: 'credit')&.invoiceable
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/payment_requests/payments/adyen_create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Payments
class AdyenCreateJob < ApplicationJob
queue_as 'providers'

unique :until_executed
unique :until_executed_patch

retry_on Faraday::ConnectionFailed, wait: :polynomially_longer, attempts: 6

Expand Down
2 changes: 1 addition & 1 deletion app/jobs/payment_requests/payments/stripe_create_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Payments
class StripeCreateJob < ApplicationJob
queue_as 'providers'

unique :until_executed, on_conflict: :log
unique :until_executed_patch, on_conflict: :log

retry_on ::Stripe::RateLimitError, wait: :polynomially_longer, attempts: 6
retry_on ::Stripe::APIConnectionError, wait: :polynomially_longer, attempts: 6
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/wallets/refresh_ongoing_balance_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Wallets
class RefreshOngoingBalanceJob < ApplicationJob
queue_as 'wallets'

unique :until_executed, on_conflict: :log
unique :until_executed_patch, on_conflict: :log

retry_on ActiveRecord::StaleObjectError, wait: :polynomially_longer, attempts: 6

Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ class Application < Rails::Application
config.active_support.cache_format_version = 7.1
end
end

require_relative "../lib/active_job/uniqueness/strategies/until_executed_patch"
20 changes: 20 additions & 0 deletions lib/active_job/uniqueness/strategies/until_executed_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require "active_job/uniqueness/strategies/until_executed"

module ActiveJob
module Uniqueness
module Strategies
class UntilExecutedPatch < UntilExecuted
def before_enqueue
return if lock(resource: lock_key, ttl: lock_ttl)
# We're retrying the job, so we don't need to lock again
return if job.executions > 0

handle_conflict(resource: lock_key, on_conflict: on_conflict)
abort_job
end
end
end
end
end