Skip to content

Commit

Permalink
Make sure batch is still first arg of the batch callback
Browse files Browse the repository at this point in the history
* Add spec for adding arguments and options to the batch callback
  • Loading branch information
jpcamara committed Sep 17, 2024
1 parent 78c5dcf commit 9dee371
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/models/solid_queue/job_batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def finish
if on_finish_active_job.present?
active_job = ActiveJob::Base.deserialize(on_finish_active_job)
active_job.send(:deserialize_arguments_if_needed)
active_job.arguments = [self] + Array.wrap(active_job.arguments)
ActiveJob.perform_all_later([active_job])
attrs[:job] = Job.find_by(active_job_id: active_job.job_id)
end
Expand Down
26 changes: 22 additions & 4 deletions test/models/solid_queue/job_batch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class SolidQueue::JobBatchTest < ActiveSupport::TestCase
SolidQueue::JobBatch.destroy_all
end

class BatchWithArgumentsJob < ApplicationJob
def perform(batch, arg1, arg2)
Rails.logger.info "Hi #{batch.id}, #{arg1}, #{arg2}!"
end
end

class NiceJob < ApplicationJob
retry_on Exception, wait: 1.second

Expand All @@ -18,14 +24,14 @@ def perform(arg)

test "batch will be completed on success" do
batch = SolidQueue::JobBatch.enqueue(on_finish: BatchCompletionJob) {}
assert_equal "success", batch.completion_type
assert_equal BatchCompletionJob.name, batch.job_class
assert_not_nil batch.on_finish_active_job
assert_equal BatchCompletionJob.name, batch.on_finish_active_job["job_class"]
end

test "batch will be completed on finish" do
batch = SolidQueue::JobBatch.enqueue(on_success: BatchCompletionJob) {}
assert_equal "success", batch.completion_type
assert_equal BatchCompletionJob.name, batch.job_class
assert_not_nil batch.on_success_active_job
assert_equal BatchCompletionJob.name, batch.on_success_active_job["job_class"]
end

test "sets the batch_id on jobs created inside of the enqueue block" do
Expand All @@ -45,4 +51,16 @@ def perform(arg)
end
assert_nil SolidQueue::JobBatch.current_batch_id
end

test "allow arguments and options for callbacks" do
SolidQueue::JobBatch.enqueue(
on_finish: BatchWithArgumentsJob.new(1, 2).set(queue: :batch),
) do
NiceJob.perform_later("world")
end

assert_not_nil SolidQueue::JobBatch.last.on_finish_active_job["arguments"]
assert_equal SolidQueue::JobBatch.last.on_finish_active_job["arguments"], [1, 2]
assert_equal SolidQueue::JobBatch.last.on_finish_active_job["queue_name"], "batch"
end
end

0 comments on commit 9dee371

Please sign in to comment.