Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always store a default queue_name, priority and scheduled_at; index by queue_name and scheduled_at #37

Merged
merged 1 commit into from Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -43,6 +43,9 @@ $ bundle install
t.integer :priority
t.jsonb :serialized_params
t.timestamp :scheduled_at

t.index :scheduled_at
t.index [:queue_name, :scheduled_at]
end
end
end
Expand Down
13 changes: 8 additions & 5 deletions lib/good_job/job.rb
Expand Up @@ -2,9 +2,12 @@ module GoodJob
class Job < ActiveRecord::Base
include Lockable

self.table_name = 'good_jobs'
DEFAULT_QUEUE_NAME = 'default'.freeze
DEFAULT_PRIORITY = 0

scope :only_scheduled, -> { where("scheduled_at < ?", Time.current).or(where(scheduled_at: nil)) }
self.table_name = 'good_jobs'.freeze

scope :only_scheduled, -> { where(arel_table['scheduled_at'].lteq(Time.current)).or(where(scheduled_at: nil)) }
scope :priority_ordered, -> { order(priority: :desc) }
scope :to_performer, -> { Performer.new(self) }

Expand All @@ -31,10 +34,10 @@ def self.enqueue(active_job, scheduled_at: nil, create_with_advisory_lock: false
good_job = nil
ActiveSupport::Notifications.instrument("enqueue_job.good_job", { active_job: active_job, scheduled_at: scheduled_at, create_with_advisory_lock: create_with_advisory_lock }) do |instrument_payload|
good_job = GoodJob::Job.new(
queue_name: active_job.queue_name,
priority: active_job.priority,
queue_name: active_job.queue_name.presence || DEFAULT_QUEUE_NAME,
priority: active_job.priority || DEFAULT_PRIORITY,
serialized_params: active_job.serialize,
scheduled_at: scheduled_at,
scheduled_at: scheduled_at || Time.current,
create_with_advisory_lock: create_with_advisory_lock
)

Expand Down
@@ -0,0 +1,6 @@
class AddIndexToGoodJobs < ActiveRecord::Migration[6.0]
def change
add_index :good_jobs, :scheduled_at
add_index :good_jobs, [:queue_name, :scheduled_at]
end
end
4 changes: 3 additions & 1 deletion spec/dummy/db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_02_24_015928) do
ActiveRecord::Schema.define(version: 2020_07_15_224305) do

# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
Expand All @@ -23,6 +23,8 @@
t.integer "priority"
t.jsonb "serialized_params"
t.datetime "scheduled_at"
t.index ["queue_name", "scheduled_at"], name: "index_good_jobs_on_queue_name_and_scheduled_at"
t.index ["scheduled_at"], name: "index_good_jobs_on_scheduled_at"
end

end