module Chore::Job::ClassMethods

Constants

DEFAULT_OPTIONS

Public Instance Methods

dedupe_key(*args) click to toggle source
# File lib/chore/job.rb, line 124
def dedupe_key(*args)
  return unless has_dedupe_lambda?

  # run the proc to get the key
  self.options[:dedupe_lambda].call(*args).to_s
end
has_backoff?() click to toggle source

We require a proc for the backoff strategy, but as we check for it in ‘.queue_options` we can simply check for the key at this point.

# File lib/chore/job.rb, line 116
def has_backoff?
  self.options.key?(:backoff)
end
has_dedupe_lambda?() click to toggle source
# File lib/chore/job.rb, line 120
def has_dedupe_lambda?
  self.options.key?(:dedupe_lambda)
end
job_hash(job_params) click to toggle source

Resque/Sidekiq compatible serialization. No reason to change what works

# File lib/chore/job.rb, line 103
def job_hash(job_params)
  {:class => self.to_s, :args => job_params}
end
perform(*args) click to toggle source

Execute the current job. We create an instance of the job to do the perform as this allows the jobs themselves to do initialization that might require access to the parameters of the job.

# File lib/chore/job.rb, line 89
def perform(*args)
  job = self.new(args)
  job.perform(*args)
end
perform_async(*args) click to toggle source

Publish a job using an instance of job. Similar to perform we do this so that a job can perform initialization logic before the perform_async is begun. This, in addition, to hooks allows for rather complex jobs to be written simply.

# File lib/chore/job.rb, line 97
def perform_async(*args)
  job = self.new(args)
  job.perform_async(*args)
end
prefixed_queue_name() click to toggle source

The name of the configured queue, combined with an optional prefix

@return [String]

# File lib/chore/job.rb, line 110
def prefixed_queue_name
  "#{Chore.config.queue_prefix}#{self.options[:name]}"
end
queue_options(opts = {}) click to toggle source

Pass a hash of options to queue_options the included class’s use of Chore::Job opts has just the one required option.

  • :name: which should map to the name of the queue this job should be published to.

# File lib/chore/job.rb, line 49
def queue_options(opts = {})
  @chore_options = (@chore_options || DEFAULT_OPTIONS).merge(opts_from_cli).merge(opts)

  required_options.each do |k|
    raise ArgumentError.new("#{self.to_s} :#{k} is a required option for Chore::Job") unless @chore_options[k]
  end

  if @chore_options.key?(:backoff)
    if !@chore_options[:backoff].is_a?(Proc)
      raise ArgumentError, "#{self.to_s}: backoff must be a lambda or Proc"
    elsif @chore_options[:backoff].arity != 1
      raise ArgumentError, "#{self.to_s}: backoff must accept a single argument"
    end
  end

  if @chore_options.key?(:dedupe_lambda)
    if !@chore_options[:dedupe_lambda].is_a?(Proc)
      raise ArgumentError, "#{self.to_s}: dedupe_lambda must be a lambda or Proc"
    end
  end
end
required_options() click to toggle source

This is a method so it can be overriden to create additional required queue_options params. This also determines what options get pulled from the global Chore.config.

# File lib/chore/job.rb, line 74
def required_options
  [:name, :publisher, :max_attempts]
end