Skip to content

Commit 9d0da03

Browse files
committed
Store expires_at time in blocked_executions
We'll use this to improve the queries performed by the concurrency maintenance task.
1 parent feef30b commit 9d0da03

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

app/models/solid_queue/blocked_execution.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module SolidQueue
22
class BlockedExecution < SolidQueue::Execution
33
assume_attributes_from_job :concurrency_key
4+
before_create :set_expires_at
45

56
has_one :semaphore, foreign_key: :key, primary_key: :concurrency_key
67

@@ -34,6 +35,10 @@ def release
3435
end
3536

3637
private
38+
def set_expires_at
39+
self.expires_at = job.concurrency_duration.from_now
40+
end
41+
3742
def acquire_concurrency_lock
3843
Semaphore.wait(job)
3944
end

db/migrate/20231103204612_create_solid_queue_concurrency_controls.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ def change
1212
t.string :concurrency_key, null: false
1313

1414
t.datetime :created_at, null: false
15+
t.datetime :expires_at, null: false
1516

1617
t.index [ :concurrency_key, :priority, :job_id ], name: "index_solid_queue_blocked_executions_for_release"
18+
t.index [ :expires_at, :concurrency_key ], name: "index_solid_queue_blocked_executions_for_maintenance"
1719
end
1820

1921
create_table :solid_queue_semaphores do |t|

test/dummy/db/schema.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
t.integer "priority", default: 0, null: false
2626
t.string "concurrency_key", null: false
2727
t.datetime "created_at", null: false
28+
t.datetime "expires_at", null: false
2829
t.index ["concurrency_key", "priority", "job_id"], name: "index_solid_queue_blocked_executions_for_release"
30+
t.index ["expires_at", "concurrency_key"], name: "index_solid_queue_blocked_executions_for_maintenance"
2931
t.index ["job_id"], name: "index_solid_queue_blocked_executions_on_job_id", unique: true
3032
end
3133

test/models/solid_queue/job_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ class NonOverlappingGroupedJob2 < NonOverlappingJob
9393
assert_blocked do
9494
NonOverlappingJob.perform_later(@result, name: "B")
9595
end
96+
97+
blocked_execution = SolidQueue::BlockedExecution.last
98+
assert blocked_execution.expires_at <= SolidQueue.default_concurrency_control_period.from_now
9699
end
97100

98101
private

0 commit comments

Comments
 (0)