Skip to content

Commit 6d38b79

Browse files
authored
fix(unique-jobs): Fix on retry in unique jobs (#2862)
## 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`
1 parent 8435425 commit 6d38b79

File tree

8 files changed

+29
-12
lines changed

8 files changed

+29
-12
lines changed

app/jobs/integrations/aggregator/invoices/crm/create_customer_association_job.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module Crm
77
class CreateCustomerAssociationJob < ApplicationJob
88
queue_as 'integrations'
99

10-
unique :until_executed, on_conflict: :log
11-
1210
retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 10
1311
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100
1412

app/jobs/integrations/aggregator/invoices/crm/create_job.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module Crm
77
class CreateJob < ApplicationJob
88
queue_as 'integrations'
99

10-
unique :until_executed, on_conflict: :log
11-
1210
retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 10
1311
retry_on Integrations::Aggregator::BasePayload::Failure, wait: :polynomially_longer, attempts: 10
1412
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100

app/jobs/integrations/aggregator/invoices/crm/update_job.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module Crm
77
class UpdateJob < ApplicationJob
88
queue_as 'integrations'
99

10-
unique :until_executed, on_conflict: :log
11-
1210
retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 10
1311
retry_on Integrations::Aggregator::BasePayload::Failure, wait: :polynomially_longer, attempts: 10
1412
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100

app/jobs/integrations/aggregator/subscriptions/crm/create_customer_association_job.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module Crm
77
class CreateCustomerAssociationJob < ApplicationJob
88
queue_as 'integrations'
99

10-
unique :until_executed, on_conflict: :log
11-
1210
retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 10
1311
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100
1412

app/jobs/integrations/aggregator/subscriptions/crm/create_job.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module Crm
77
class CreateJob < ApplicationJob
88
queue_as 'integrations'
99

10-
unique :until_executed, on_conflict: :log
11-
1210
retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 10
1311
retry_on Integrations::Aggregator::BasePayload::Failure, wait: :polynomially_longer, attempts: 10
1412
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100

app/jobs/integrations/aggregator/subscriptions/crm/update_job.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module Crm
77
class UpdateJob < ApplicationJob
88
queue_as 'integrations'
99

10-
unique :until_executed, on_conflict: :log
11-
1210
retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 10
1311
retry_on Integrations::Aggregator::BasePayload::Failure, wait: :polynomially_longer, attempts: 10
1412
retry_on RequestLimitError, wait: :polynomially_longer, attempts: 100

config/application.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ class Application < Rails::Application
4242
config.active_support.cache_format_version = 7.1
4343
end
4444
end
45+
46+
require_relative "../lib/active_job/uniqueness/strategies/until_executed_patch"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require "active_job/uniqueness/strategies/until_executed"
4+
5+
# https://github.com/veeqo/activejob-uniqueness/issues/75
6+
# retry_on does not work with until_executed strategy
7+
8+
module ActiveJob
9+
module Uniqueness
10+
module Strategies
11+
module UntilExecutedPatch
12+
def before_enqueue
13+
return if lock(resource: lock_key, ttl: lock_ttl)
14+
# We're retrying the job, so we don't need to lock again
15+
return if job.executions > 0
16+
17+
handle_conflict(resource: lock_key, on_conflict: on_conflict)
18+
abort_job
19+
end
20+
end
21+
end
22+
end
23+
end
24+
25+
ActiveJob::Uniqueness::Strategies::UntilExecuted.prepend(
26+
ActiveJob::Uniqueness::Strategies::UntilExecutedPatch
27+
)

0 commit comments

Comments
 (0)