From ed302800ec006adeb8133f702511dc9bf20cb82d Mon Sep 17 00:00:00 2001 From: Rosa Gutierrez Date: Wed, 22 Nov 2023 20:41:07 +0100 Subject: [PATCH] Fix index on `blocked_executions` for releasing and add index on semaphores Also, include `job_id` in the ORDER when releasing blocked executions. Thanks to @djmb for the suggestions. --- app/models/solid_queue/blocked_execution.rb | 1 - app/models/solid_queue/execution.rb | 2 ++ app/models/solid_queue/ready_execution.rb | 1 - .../20231103204612_create_solid_queue_concurrency_controls.rb | 4 +++- test/dummy/db/schema.rb | 3 ++- 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/models/solid_queue/blocked_execution.rb b/app/models/solid_queue/blocked_execution.rb index 40bdfb38..58fcb7a3 100644 --- a/app/models/solid_queue/blocked_execution.rb +++ b/app/models/solid_queue/blocked_execution.rb @@ -5,7 +5,6 @@ class BlockedExecution < SolidQueue::Execution has_one :semaphore, foreign_key: :key, primary_key: :concurrency_key scope :releasable, -> { left_outer_joins(:semaphore).merge(Semaphore.available.or(Semaphore.where(id: nil))) } - scope :ordered, -> { order(priority: :asc) } class << self def unblock(count) diff --git a/app/models/solid_queue/execution.rb b/app/models/solid_queue/execution.rb index 366c4e24..76b9a8a9 100644 --- a/app/models/solid_queue/execution.rb +++ b/app/models/solid_queue/execution.rb @@ -4,6 +4,8 @@ class Execution < SolidQueue::Record self.abstract_class = true + scope :ordered, -> { order(priority: :asc, job_id: :asc) } + belongs_to :job alias_method :discard, :destroy diff --git a/app/models/solid_queue/ready_execution.rb b/app/models/solid_queue/ready_execution.rb index 7701d815..6928781b 100644 --- a/app/models/solid_queue/ready_execution.rb +++ b/app/models/solid_queue/ready_execution.rb @@ -1,7 +1,6 @@ module SolidQueue class ReadyExecution < Execution scope :queued_as, ->(queue_name) { where(queue_name: queue_name) } - scope :ordered, -> { order(priority: :asc, job_id: :asc) } assume_attributes_from_job diff --git a/db/migrate/20231103204612_create_solid_queue_concurrency_controls.rb b/db/migrate/20231103204612_create_solid_queue_concurrency_controls.rb index 4c6a48e1..ca4124de 100644 --- a/db/migrate/20231103204612_create_solid_queue_concurrency_controls.rb +++ b/db/migrate/20231103204612_create_solid_queue_concurrency_controls.rb @@ -13,7 +13,7 @@ def change t.datetime :created_at, null: false - t.index [ :priority, :concurrency_key, :queue_name, :job_id ], name: "index_solid_queue_blocked_executions_for_release" + t.index [ :priority, :concurrency_key, :job_id ], name: "index_solid_queue_blocked_executions_for_release" end create_table :solid_queue_semaphores do |t| @@ -22,6 +22,8 @@ def change t.datetime :expires_at, null: false, index: true t.timestamps + + t.index [ :key, :value ] end end end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 54252c8d..bf80c155 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -26,7 +26,7 @@ t.string "concurrency_key", null: false t.datetime "created_at", null: false t.index ["job_id"], name: "index_solid_queue_blocked_executions_on_job_id", unique: true - t.index ["priority", "concurrency_key", "queue_name", "job_id"], name: "index_solid_queue_blocked_executions_for_release" + t.index ["priority", "concurrency_key", "job_id"], name: "index_solid_queue_blocked_executions_for_release" end create_table "solid_queue_claimed_executions", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| @@ -102,6 +102,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["expires_at"], name: "index_solid_queue_semaphores_on_expires_at" + t.index ["key", "value"], name: "index_solid_queue_semaphores_on_key_and_value" t.index ["key"], name: "index_solid_queue_semaphores_on_key", unique: true end