Skip to content

Commit

Permalink
fix(unique-jobs): Fix on retry in unique jobs (#2862)
Browse files Browse the repository at this point in the history
## Context

`retry_on` does not work with `until_executed` strategy.
veeqo/activejob-uniqueness#75

## Description

This PR updates all the jobs that are affected with this issue - a
combination of `retry_on` and `unique :until_executed`
  • Loading branch information
ivannovosad authored Nov 25, 2024
1 parent 8435425 commit 6d38b79
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 12 deletions.
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
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: 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"
27 changes: 27 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,27 @@
# frozen_string_literal: true

require "active_job/uniqueness/strategies/until_executed"

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

module ActiveJob
module Uniqueness
module Strategies
module UntilExecutedPatch
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

ActiveJob::Uniqueness::Strategies::UntilExecuted.prepend(
ActiveJob::Uniqueness::Strategies::UntilExecutedPatch
)

0 comments on commit 6d38b79

Please sign in to comment.