Module: Chore::Job::ClassMethods
- Defined in:
- lib/chore/job.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ }
Instance Method Summary collapse
- #dedupe_key(*args) ⇒ Object
-
#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.
- #has_dedupe_lambda? ⇒ Boolean
-
#job_hash(job_params) ⇒ Object
Resque/Sidekiq compatible serialization.
-
#options ⇒ Object
:nodoc:#.
-
#opts_from_cli ⇒ Object
:nodoc:#.
-
#perform(*args) ⇒ Object
Execute the current job.
-
#perform_async(*args) ⇒ Object
Publish a job using an instance of job.
-
#prefixed_queue_name ⇒ String
The name of the configured queue, combined with an optional prefix.
-
#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. -
#required_options ⇒ Object
This is a method so it can be overriden to create additional required queue_options params.
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.[: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.
116 117 118 |
# File 'lib/chore/job.rb', line 116 def has_backoff? self..key?(:backoff) end |
#has_dedupe_lambda? ⇒ Boolean
120 121 122 |
# File 'lib/chore/job.rb', line 120 def has_dedupe_lambda? self..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 |
#options ⇒ Object
:nodoc:#
78 79 80 |
# File 'lib/chore/job.rb', line 78 def #:nodoc:# @chore_options ||= end |
#opts_from_cli ⇒ Object
: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| .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_name ⇒ String
The name of the configured queue, combined with an optional prefix
110 111 112 |
# File 'lib/chore/job.rb', line 110 def prefixed_queue_name "#{Chore.config.queue_prefix}#{self.[: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 (opts = {}) @chore_options = (@chore_options || DEFAULT_OPTIONS).merge(opts_from_cli).merge(opts) .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 ⇒ Object
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 [:name, :publisher, :max_attempts] end |