Module: Leveret::Job::ClassMethods

Defined in:
lib/leveret/job.rb

Overview

Class methods to mixin with your job

Instance Method Summary collapse

Instance Method Details

#enqueue(params = {}) ⇒ Object

Place a job onto the queue for processing by a worker.

Parameters:

  • params (Hash) (defaults to: {})

    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

Options Hash (params):

  • :priority (Symbol) — default: job_options[:priority]

    Override the class-level priority for this job only

  • :queue_name (String) — default: job_options[:queue_name]

    Override the class-level queue name for this job only.



136
137
138
139
140
141
142
143
144
# 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

#job_optionsHash

Returns The current set of options for this job, the queue_name and priority.

Returns:

  • (Hash)

    The current set of options for this job, the queue_name and priority.



121
122
123
124
125
126
# File 'lib/leveret/job.rb', line 121

def job_options
  @job_options ||= {
    queue_name: Leveret.configuration.default_queue_name,
    priority: :normal
  }
end

#perform(params = Parameters.new) ⇒ Symbol

Shorthand to intialize a new job and run it with error handling

Parameters:

  • params (Parameters) (defaults to: Parameters.new)

    Parameters to pass to the job for execution

Returns:

  • (Symbol)

    :success, :requeue or :reject depending on job execution



102
103
104
# File 'lib/leveret/job.rb', line 102

def perform(params = Parameters.new)
  new(params).run
end

#priority(priority) ⇒ Object

Set a custom priority for this job

Parameters:



116
117
118
# File 'lib/leveret/job.rb', line 116

def priority(priority)
  job_options[:priority] = priority
end

#queue(q_name = nil) ⇒ Queue

Returns Cached Queue object for publishing jobs.

Parameters:

  • The (q_name)

    name of the queue we want to cache. If nil it’ll use the name defined in job_options[:queue_name]

Returns:

  • (Queue)

    Cached Queue object for publishing jobs



151
152
153
154
155
# 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

#queue_name(name) ⇒ Object

Set a custom queue for this job

Parameters:

  • name (String)

    Name of the queue to assign this job to



109
110
111
# File 'lib/leveret/job.rb', line 109

def queue_name(name)
  job_options[:queue_name] = name
end