Skip to content

Instantly share code, notes, and snippets.

@philsmy
Created October 15, 2017 04:55
Show Gist options
  • Save philsmy/960cc61592da118c3283a0bd4d49ee17 to your computer and use it in GitHub Desktop.
Save philsmy/960cc61592da118c3283a0bd4d49ee17 to your computer and use it in GitHub Desktop.
configuring ActiveJob and DelayedJob to have priority
# app/jobs/sample_job.rb
class SampleJob < ActiveJob::Base
queue_as :maintenance
attr_accessor :options
def enqueue(options = {})
self.options = options
super
end
def perform(thing1, thing2)
# do stuff
end
end
# config/initializers/delayed_job.rb
# store the real id of the delayed job
# pass along priority
module ActiveJob
module Core
# ID optionally provided by adapter
attr_accessor :provider_job_id
end
module QueueAdapters
class DelayedJobAdapter
class << self
def enqueue(job) #:nodoc:
priority = (job.options && job.options[:priority].present?) ? job.options[:priority] : 0
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, priority: priority)
job.provider_job_id = delayed_job.id
delayed_job
end
def enqueue_at(job, timestamp) #:nodoc:
priority = (job.options && job.options[:priority].present?) ? job.options[:priority] : 0
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, run_at: Time.at(timestamp), priority: priority)
job.provider_job_id = delayed_job.id
delayed_job
end
end
class JobWrapper #:nodoc:
attr_accessor :job_data
def initialize(job_data)
@job_data = job_data
end
def perform
Base.execute(job_data)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment