Module: Chore::Job::ClassMethods

Defined in:
lib/chore/job.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ }

Instance Method Summary collapse

Instance Method Details

#dedupe_key(*args) ⇒ Object



124
125
126
127
128
129
# 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?Boolean

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.

Returns:

  • (Boolean)


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

def has_backoff?
  self.options.key?(:backoff)
end

#has_dedupe_lambda?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/chore/job.rb', line 120

def has_dedupe_lambda?
  self.options.key?(:dedupe_lambda)
end

#job_hash(job_params) ⇒ Object

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



103
104
105
# File 'lib/chore/job.rb', line 103

def job_hash(job_params)
  {:class => self.to_s, :args => job_params}
end

#optionsObject

:nodoc:#



78
79
80
# File 'lib/chore/job.rb', line 78

def options #:nodoc:#
  @chore_options ||= queue_options
end

#opts_from_cliObject

:nodoc:#



82
83
84
# File 'lib/chore/job.rb', line 82

def opts_from_cli #:nodoc:#
  @from_cli ||= (Chore.config.marshal_dump.select {|k,v| required_options.include? k } || {})
end

#perform(*args) ⇒ Object

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.



89
90
91
92
# File 'lib/chore/job.rb', line 89

def perform(*args)
  job = self.new(args)
  job.perform(*args)
end

#perform_async(*args) ⇒ Object

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.



97
98
99
100
# File 'lib/chore/job.rb', line 97

def perform_async(*args)
  job = self.new(args)
  job.perform_async(*args)
end

#prefixed_queue_nameString

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

Returns:

  • (String)


110
111
112
# File 'lib/chore/job.rb', line 110

def prefixed_queue_name
  "#{Chore.config.queue_prefix}#{self.options[:name]}"
end

#queue_options(opts = {}) ⇒ Object

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.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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_optionsObject

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.



74
75
76
# File 'lib/chore/job.rb', line 74

def required_options
  [:name, :publisher, :max_attempts]
end