Skip to content

Commit 02d9603

Browse files
committed
Make sure batch is still first arg of the batch callback
* Add spec for adding arguments and options to the batch callback
1 parent c2a7f98 commit 02d9603

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

app/models/solid_queue/job_batch.rb

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def finish
8282
if on_finish_active_job.present?
8383
active_job = ActiveJob::Base.deserialize(on_finish_active_job)
8484
active_job.send(:deserialize_arguments_if_needed)
85+
active_job.arguments = [self] + Array.wrap(active_job.arguments)
8586
ActiveJob.perform_all_later([active_job])
8687
attrs[:job] = Job.find_by(active_job_id: active_job.job_id)
8788
end

test/models/solid_queue/job_batch_test.rb

+22-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ class SolidQueue::JobBatchTest < ActiveSupport::TestCase
88
SolidQueue::JobBatch.destroy_all
99
end
1010

11+
class BatchWithArgumentsJob < ApplicationJob
12+
def perform(batch, arg1, arg2)
13+
Rails.logger.info "Hi #{batch.id}, #{arg1}, #{arg2}!"
14+
end
15+
end
16+
1117
class NiceJob < ApplicationJob
1218
retry_on Exception, wait: 1.second
1319

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

1925
test "batch will be completed on success" do
2026
batch = SolidQueue::JobBatch.enqueue(on_finish: BatchCompletionJob) {}
21-
assert_equal "success", batch.completion_type
22-
assert_equal BatchCompletionJob.name, batch.job_class
27+
assert_not_nil batch.on_finish_active_job
28+
assert_equal BatchCompletionJob.name, batch.on_finish_active_job["job_class"]
2329
end
2430

2531
test "batch will be completed on finish" do
2632
batch = SolidQueue::JobBatch.enqueue(on_success: BatchCompletionJob) {}
27-
assert_equal "success", batch.completion_type
28-
assert_equal BatchCompletionJob.name, batch.job_class
33+
assert_not_nil batch.on_success_active_job
34+
assert_equal BatchCompletionJob.name, batch.on_success_active_job["job_class"]
2935
end
3036

3137
test "sets the batch_id on jobs created inside of the enqueue block" do
@@ -45,4 +51,16 @@ def perform(arg)
4551
end
4652
assert_nil SolidQueue::JobBatch.current_batch_id
4753
end
54+
55+
test "allow arguments and options for callbacks" do
56+
SolidQueue::JobBatch.enqueue(
57+
on_finish: BatchWithArgumentsJob.new(1, 2).set(queue: :batch),
58+
) do
59+
NiceJob.perform_later("world")
60+
end
61+
62+
assert_not_nil SolidQueue::JobBatch.last.on_finish_active_job["arguments"]
63+
assert_equal SolidQueue::JobBatch.last.on_finish_active_job["arguments"], [1, 2]
64+
assert_equal SolidQueue::JobBatch.last.on_finish_active_job["queue_name"], "batch"
65+
end
4866
end

0 commit comments

Comments
 (0)