Skip to content

Commit 54476e6

Browse files
committed
Make it so metadata is more ergonomic to include
* GoodJob and Sidekiq do not require an explicit keyword, they just accept any other arguments as metadata and set them that way * Also make it so we always return a hash for metadata, and that hash is with_indifferent_access
1 parent 230563e commit 54476e6

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

app/models/solid_queue/batch.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def on_finish=(value)
4848
super(serialize_callback(value))
4949
end
5050

51+
def metadata
52+
(super || {}).with_indifferent_access
53+
end
54+
5155
def check_completion!
5256
return if finished? || !ready?
5357
return if batch_executions.limit(1).exists?
@@ -124,7 +128,7 @@ def start_batch
124128
end
125129

126130
class << self
127-
def enqueue(on_success: nil, on_failure: nil, on_finish: nil, metadata: nil, &block)
131+
def enqueue(on_success: nil, on_failure: nil, on_finish: nil, **metadata, &block)
128132
new.tap do |batch|
129133
batch.assign_attributes(
130134
on_success: on_success,

test/integration/batch_lifecycle_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,10 @@ def perform
274274

275275
test "batch interface" do
276276
batch = SolidQueue::Batch.enqueue(
277-
metadata: { source: "test", priority: "high", user_id: 123 },
278277
on_finish: OnFinishJob,
279278
on_success: OnSuccessJob,
280-
on_failure: OnFailureJob
279+
on_failure: OnFailureJob,
280+
source: "test", priority: "high", user_id: 123
281281
) do
282282
AddToBufferJob.perform_later "hey"
283283
end

test/models/solid_queue/batch_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def perform(arg)
2424

2525
test "batch will be completed on success" do
2626
batch = SolidQueue::Batch.enqueue(on_finish: BatchCompletionJob) { }
27-
job_batch = SolidQueue::Batch.find_by(batch_id: batch.batch_id)
27+
job_batch = SolidQueue::Batch.find_by(id: batch.id)
2828
assert_not_nil job_batch.on_finish
2929
assert_equal BatchCompletionJob.name, job_batch.on_finish["job_class"]
3030
end
3131

3232
test "batch will be completed on finish" do
3333
batch = SolidQueue::Batch.enqueue(on_success: BatchCompletionJob) { }
34-
job_batch = SolidQueue::Batch.find_by(batch_id: batch.batch_id)
34+
job_batch = SolidQueue::Batch.find_by(id: batch.id)
3535
assert_not_nil job_batch.on_success
3636
assert_equal BatchCompletionJob.name, job_batch.on_success["job_class"]
3737
end
@@ -43,7 +43,7 @@ def perform(arg)
4343
end
4444

4545
assert_equal 2, SolidQueue::Job.count
46-
assert_equal [ batch.batch_id ] * 2, SolidQueue::Job.last(2).map(&:batch_id)
46+
assert_equal [ batch.id ] * 2, SolidQueue::Job.last(2).map(&:batch_id)
4747
end
4848

4949
test "batch id is present inside the block" do
@@ -68,7 +68,7 @@ def perform(arg)
6868

6969
test "creates batch with metadata" do
7070
SolidQueue::Batch.enqueue(
71-
metadata: { source: "test", priority: "high", user_id: 123 }
71+
source: "test", priority: "high", user_id: 123
7272
) do
7373
NiceJob.perform_later("world")
7474
end

0 commit comments

Comments
 (0)