Skip to content

Commit

Permalink
Ensure concurrency controls are ignored when perform_now inside ano…
Browse files Browse the repository at this point in the history
…ther job
  • Loading branch information
bensheldon committed Apr 23, 2024
1 parent 410b34f commit 46c7d57
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
10 changes: 10 additions & 0 deletions demo/app/jobs/example_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ class ExampleJob < ApplicationJob

retry_on(DeadError, attempts: 3) { nil }

include GoodJob::ActiveJobExtensions::Concurrency
good_job_control_concurrency_with(
total_limit: 1, # Maximum number of unfinished jobs to allow with the concurrency key
key: -> do
puts job_id
foo = arguments.first
"#{self.class.name}_#{foo}"
end
)

class BatchJob < ApplicationJob
class CallbackJob < ApplicationJob
def perform(batch, params)
Expand Down
2 changes: 1 addition & 1 deletion lib/good_job/active_job_extensions/concurrency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def deserialize(job_data)
key = job.good_job_concurrency_key
next if key.blank?

if CurrentThread.execution.blank?
if CurrentThread.execution.blank? || CurrentThread.execution.active_job_id != job_id
logger.debug("Ignoring concurrency limits because the job is executed with `perform_now`.")
next
end
Expand Down
1 change: 1 addition & 0 deletions sorbet/rbi/todo.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module ::TestJob::Error; end
module ::TestJob::ExpectedError; end
module ::TestJob::RunError; end
module ::TestJob::SuccessCallbackJob; end
module ::WrapperJob; end
module GoodJob::Job::ERROR_EVENT_INTERRUPTED; end
module GoodJob::Job::ERROR_EVENT_RETRIED; end
module Rails::Server; end
12 changes: 12 additions & 0 deletions spec/lib/good_job/active_job_extensions/concurrency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ def perform(name:)
TestJob.perform_now(name: "Alice")
expect(JOB_PERFORMED).to be_true
end

it 'is ignored when the job is executed inside another job' do
stub_const("WrapperJob", Class.new(ApplicationJob) do
def perform
TestJob.perform_now(name: "Alice")
end
end)

WrapperJob.perform_later
GoodJob.perform_inline
expect(JOB_PERFORMED).to be_true
end
end

describe '#enqueue_throttle' do
Expand Down

0 comments on commit 46c7d57

Please sign in to comment.