Skip to content

Commit 230563e

Browse files
committed
Switch batch_id to active_job_batch_id
* This is a placeholder for a later rails-based abstraction * Use bigint references like everything else and directly relate to the record
1 parent a10904f commit 230563e

File tree

8 files changed

+31
-33
lines changed

8 files changed

+31
-33
lines changed

app/models/solid_queue/batch.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ module SolidQueue
44
class Batch < Record
55
include Trackable
66

7-
has_many :jobs, foreign_key: :batch_id, primary_key: :batch_id
8-
has_many :batch_executions, foreign_key: :batch_id, primary_key: :batch_id, class_name: "SolidQueue::BatchExecution",
9-
dependent: :destroy
7+
has_many :jobs
8+
has_many :batch_executions, class_name: "SolidQueue::BatchExecution", dependent: :destroy
109

1110
serialize :on_finish, coder: JSON
1211
serialize :on_success, coder: JSON
1312
serialize :on_failure, coder: JSON
1413
serialize :metadata, coder: JSON
1514

16-
after_initialize :set_batch_id
15+
after_initialize :set_active_job_batch_id
1716
after_commit :start_batch, on: :create, unless: -> { ActiveRecord.respond_to?(:after_all_transactions_commit) }
1817

1918
mattr_accessor :maintenance_queue_name
@@ -25,7 +24,7 @@ def enqueue(&block)
2524
transaction do
2625
save! if new_record?
2726

28-
Batch.wrap_in_batch_context(batch_id) do
27+
Batch.wrap_in_batch_context(id) do
2928
block&.call(self)
3029
end
3130

@@ -54,7 +53,7 @@ def check_completion!
5453
return if batch_executions.limit(1).exists?
5554

5655
rows = Batch
57-
.by_batch_id(batch_id)
56+
.where(id: id)
5857
.unfinished
5958
.empty_executions
6059
.update_all(finished_at: Time.current)
@@ -77,8 +76,8 @@ def check_completion!
7776

7877
private
7978

80-
def set_batch_id
81-
self.batch_id ||= SecureRandom.uuid
79+
def set_active_job_batch_id
80+
self.active_job_batch_id ||= SecureRandom.uuid
8281
end
8382

8483
def as_active_job(active_job_klass)
@@ -114,7 +113,7 @@ def execute_callbacks
114113
end
115114

116115
def enqueue_empty_job
117-
Batch.wrap_in_batch_context(batch_id) do
116+
Batch.wrap_in_batch_context(id) do
118117
EmptyJob.set(queue: self.class.maintenance_queue_name || "default").perform_later
119118
end
120119
end

app/models/solid_queue/batch/trackable.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ module Trackable
1010
scope :succeeded, -> { finished.where(failed_at: nil) }
1111
scope :unfinished, -> { where(finished_at: nil) }
1212
scope :failed, -> { where.not(failed_at: nil) }
13-
scope :by_batch_id, ->(batch_id) { where(batch_id:) }
1413
scope :empty_executions, -> {
1514
where(<<~SQL)
1615
NOT EXISTS (
1716
SELECT 1 FROM solid_queue_batch_executions
18-
WHERE solid_queue_batch_executions.batch_id = solid_queue_batches.batch_id
17+
WHERE solid_queue_batch_executions.batch_id = solid_queue_batches.id
1918
LIMIT 1
2019
)
2120
SQL

app/models/solid_queue/batch_execution.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
module SolidQueue
44
class BatchExecution < Record
55
belongs_to :job, optional: true
6-
belongs_to :batch, foreign_key: :batch_id, primary_key: :batch_id
6+
belongs_to :batch
77

88
after_commit :check_completion, on: :destroy
99

1010
private
1111
def check_completion
12-
batch = Batch.find_by(batch_id: batch_id)
12+
batch = Batch.find_by(id: batch_id)
1313
batch.check_completion! if batch.present?
1414
end
1515

@@ -24,7 +24,7 @@ def create_all_from_jobs(jobs)
2424
})
2525

2626
total = jobs.size
27-
SolidQueue::Batch.where(batch_id:).update_all([ "total_jobs = total_jobs + ?", total ])
27+
SolidQueue::Batch.where(id: batch_id).update_all([ "total_jobs = total_jobs + ?", total ])
2828
end
2929
end
3030
end

app/models/solid_queue/job/batchable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Batchable
66
extend ActiveSupport::Concern
77

88
included do
9-
belongs_to :batch, foreign_key: :batch_id, primary_key: :batch_id, class_name: "SolidQueue::Batch", optional: true
9+
belongs_to :batch, optional: true
1010
has_one :batch_execution, foreign_key: :job_id, dependent: :destroy
1111

1212
after_create :create_batch_execution, if: :batch_id?

lib/active_job/batch_id.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def deserialize(job_data)
2525
end
2626

2727
def batch
28-
@batch ||= SolidQueue::Batch.find_by(batch_id: batch_id)
28+
@batch ||= SolidQueue::Batch.find_by(id: batch_id)
2929
end
3030

3131
private

lib/generators/solid_queue/install/templates/db/queue_schema.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
t.string "concurrency_key"
3838
t.datetime "created_at", null: false
3939
t.datetime "updated_at", null: false
40-
t.string "batch_id"
40+
t.bigint "batch_id"
4141
t.index [ "active_job_id" ], name: "index_solid_queue_jobs_on_active_job_id"
4242
t.index [ "batch_id" ], name: "index_solid_queue_jobs_on_batch_id"
4343
t.index [ "class_name" ], name: "index_solid_queue_jobs_on_class_name"
@@ -123,7 +123,7 @@
123123
end
124124

125125
create_table "solid_queue_batches", force: :cascade do |t|
126-
t.string "batch_id"
126+
t.string "active_job_batch_id"
127127
t.text "on_finish"
128128
t.text "on_success"
129129
t.text "on_failure"
@@ -136,12 +136,12 @@
136136
t.datetime "failed_at"
137137
t.datetime "created_at", null: false
138138
t.datetime "updated_at", null: false
139-
t.index ["batch_id"], name: "index_solid_queue_batches_on_batch_id", unique: true
139+
t.index ["active_job_batch_id"], name: "index_solid_queue_batches_on_active_job_batch_id", unique: true
140140
end
141141

142142
create_table "solid_queue_batch_executions", force: :cascade do |t|
143143
t.bigint "job_id", null: false
144-
t.string "batch_id", null: false
144+
t.bigint "batch_id", null: false
145145
t.datetime "created_at", null: false
146146
t.index [ "job_id" ], name: "index_solid_queue_batch_executions_on_job_id", unique: true
147147
t.index ["batch_id"], name: "index_solid_queue_batch_executions_on_batch_id"

test/dummy/db/queue_schema.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
t.string "concurrency_key"
5050
t.datetime "created_at", null: false
5151
t.datetime "updated_at", null: false
52-
t.string "batch_id"
52+
t.bigint "batch_id"
5353
t.index ["active_job_id"], name: "index_solid_queue_jobs_on_active_job_id"
5454
t.index ["batch_id"], name: "index_solid_queue_jobs_on_batch_id"
5555
t.index ["class_name"], name: "index_solid_queue_jobs_on_class_name"
@@ -135,7 +135,7 @@
135135
end
136136

137137
create_table "solid_queue_batches", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
138-
t.string "batch_id"
138+
t.string "active_job_batch_id"
139139
t.text "on_finish"
140140
t.text "on_success"
141141
t.text "on_failure"
@@ -148,12 +148,12 @@
148148
t.datetime "failed_at"
149149
t.datetime "created_at", null: false
150150
t.datetime "updated_at", null: false
151-
t.index ["batch_id"], name: "index_solid_queue_batches_on_batch_id", unique: true
151+
t.index ["active_job_batch_id"], name: "index_solid_queue_batches_on_active_job_batch_id", unique: true
152152
end
153153

154154
create_table "solid_queue_batch_executions", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
155155
t.bigint "job_id", null: false
156-
t.string "batch_id", null: false
156+
t.bigint "batch_id", null: false
157157
t.datetime "created_at", null: false
158158
t.index ["job_id"], name: "index_solid_queue_batch_executions_on_job_id", unique: true
159159
t.index ["batch_id"], name: "index_solid_queue_batch_executions_on_batch_id"

test/integration/batch_lifecycle_test.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ def perform
173173
wait_for_batches_to_finish_for(3.seconds)
174174
wait_for_jobs_to_finish_for(1.second)
175175

176-
job_batch1 = SolidQueue::Batch.find_by(batch_id: batch1.batch_id)
177-
job_batch2 = SolidQueue::Batch.find_by(batch_id: batch2.batch_id)
176+
job_batch1 = SolidQueue::Batch.find_by(id: batch1.id)
177+
job_batch2 = SolidQueue::Batch.find_by(id: batch2.id)
178178

179179
assert_equal 2, SolidQueue::Batch.count
180180
assert_equal 2, SolidQueue::Batch.finished.count
@@ -232,8 +232,8 @@ def perform
232232
wait_for_batches_to_finish_for(3.seconds)
233233
wait_for_jobs_to_finish_for(1.second)
234234

235-
job_batch1 = SolidQueue::Batch.find_by(batch_id: batch1.batch_id)
236-
job_batch2 = SolidQueue::Batch.find_by(batch_id: batch2.batch_id)
235+
job_batch1 = SolidQueue::Batch.find_by(id: batch1.id)
236+
job_batch2 = SolidQueue::Batch.find_by(id: batch2.id)
237237

238238
assert_equal 2, SolidQueue::Batch.count
239239
assert_equal 2, SolidQueue::Batch.finished.count
@@ -288,7 +288,7 @@ def perform
288288
wait_for_batches_to_finish_for(2.seconds)
289289
wait_for_jobs_to_finish_for(1.second)
290290

291-
assert_equal [ "Hi finish #{batch.batch_id}!", "Hi success #{batch.batch_id}!", "hey" ].sort, JobBuffer.values.sort
291+
assert_equal [ "Hi finish #{batch.id}!", "Hi success #{batch.id}!", "hey" ].sort, JobBuffer.values.sort
292292
assert_equal 1, batch.reload.completed_jobs
293293
assert_equal 0, batch.failed_jobs
294294
assert_equal 0, batch.pending_jobs
@@ -299,23 +299,23 @@ class OnFinishJob < ApplicationJob
299299
queue_as :background
300300

301301
def perform(batch)
302-
JobBuffer.add "Hi finish #{batch.batch_id}!"
302+
JobBuffer.add "Hi finish #{batch.id}!"
303303
end
304304
end
305305

306306
class OnSuccessJob < ApplicationJob
307307
queue_as :background
308308

309309
def perform(batch)
310-
JobBuffer.add "Hi success #{batch.batch_id}!"
310+
JobBuffer.add "Hi success #{batch.id}!"
311311
end
312312
end
313313

314314
class OnFailureJob < ApplicationJob
315315
queue_as :background
316316

317317
def perform(batch)
318-
JobBuffer.add "Hi failure #{batch.batch_id}!"
318+
JobBuffer.add "Hi failure #{batch.id}!"
319319
end
320320
end
321321

@@ -330,6 +330,6 @@ def job!(active_job)
330330
end
331331

332332
def batch_jobs(*batches)
333-
SolidQueue::Job.where(batch_id: batches.map(&:batch_id))
333+
SolidQueue::Job.where(id: batches.map(&:id))
334334
end
335335
end

0 commit comments

Comments
 (0)