module Leveret::Job::ClassMethods
Class methods to mixin with your job
Public Instance Methods
Place a job onto the queue for processing by a worker.
@param [Hash] params The parameters to be included with the work request. These can be anything, however
the keys +:priority+ and +:queue_name+ are reserved for customising those aspects of the job's execution
@option params [Symbol] :priority (job_options) Override the class-level priority for this job only @option params [String] :queue_name (job_options) Override the class-level queue name for this
job only.
# File lib/leveret/job.rb, line 136 def enqueue(params = {}) priority = params.delete(:priority) || job_options[:priority] q_name = params.delete(:queue_name) || job_options[:queue_name] Leveret.log.info "Queuing #{name} to #{q_name} (#{priority}) with #{params}" payload = { job: self.name, params: params } queue(q_name).publish(payload, priority: priority) end
@return [Hash] The current set of options for this job, the queue_name
and priority
.
# File lib/leveret/job.rb, line 121 def job_options @job_options ||= { queue_name: Leveret.configuration.default_queue_name, priority: :normal } end
Shorthand to intialize a new job and run it with error handling
@param [Parameters] params Parameters
to pass to the job for execution
@return [Symbol] :success, :requeue or :reject depending on job execution
# File lib/leveret/job.rb, line 102 def perform(params = Parameters.new) new(params).run end
Set a custom priority for this job
@param [Symbol] priority Priority for this job, see {Queue::PRIORITY_MAP} for details
# File lib/leveret/job.rb, line 116 def priority(priority) job_options[:priority] = priority end
@private Cache the queue for this job
@param [q_name] The name of the queue we want to cache. If nil it'll use the name defined in
+job_options[:queue_name]+
@return [Queue] Cached Queue
object for publishing jobs
# File lib/leveret/job.rb, line 151 def queue(q_name = nil) q_name ||= job_options[:queue_name] @queue ||= {} @queue[q_name] ||= Leveret::Queue.new(q_name) end
Set a custom queue for this job
@param [String] name Name of the queue to assign this job to
# File lib/leveret/job.rb, line 109 def queue_name(name) job_options[:queue_name] = name end