Module: Wurk::Job::Options::ClassMethods

Included in:
Worker::ClassMethods
Defined in:
lib/wurk/job/options.rb

Overview

Also the options half of Worker::ClassMethods, so an ActiveJob class configuring a Wurk option is told about a bad value at the same place a plain worker is.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sidekiq_options_hash ⇒ Object



60
61
62
# File 'lib/wurk/job/options.rb', line 60

def sidekiq_options_hash
  get_sidekiq_options
end

#sidekiq_retries_exhausted_block ⇒ Object (readonly)

Returns the value of attribute sidekiq_retries_exhausted_block.



66
67
68
# File 'lib/wurk/job/options.rb', line 66

def sidekiq_retries_exhausted_block
  @sidekiq_retries_exhausted_block
end

#sidekiq_retry_in_block ⇒ Object (readonly)

Returns the value of attribute sidekiq_retry_in_block.



66
67
68
# File 'lib/wurk/job/options.rb', line 66

def sidekiq_retry_in_block
  @sidekiq_retry_in_block
end

Instance Method Details

#get_sidekiq_options ⇒ Object

Sidekiq's public API name — must stay get_sidekiq_options.



56
57
58
# File 'lib/wurk/job/options.rb', line 56

def get_sidekiq_options # rubocop:disable Naming/AccessorMethodName
  @sidekiq_options_hash ||= inherited_sidekiq_options # rubocop:disable Naming/MemoizedInstanceVariableName
end

#inherited(subclass) ⇒ Object



76
77
78
79
80
81
# File 'lib/wurk/job/options.rb', line 76

def inherited(subclass)
  super
  subclass.instance_variable_set(:@sidekiq_options_hash, get_sidekiq_options.dup)
  inherit_ivar(subclass, :@sidekiq_retry_in_block)
  inherit_ivar(subclass, :@sidekiq_retries_exhausted_block)
end

#sidekiq_options(opts = {}) ⇒ Hash

Set per-class job options (merged over any inherited options).

Examples:

sidekiq_options queue: "mailers", retry: 3, unique_for: 10.minutes

Opt into Wurk::Status tracking

sidekiq_options track: true

Bound one attempt, and the job as a whole

sidekiq_options timeout: 30, deadline: 5.minutes

Collapse a burst of enqueues into one job

sidekiq_options collapse: { policy: :debounce, wait: 5, max_wait: 60 }

Parameters:

  • opts (Hash) (defaults to: {}) —

    any of queue:, retry:, dead:, backtrace:, expires_in:, tags:, pool:, unique_for:, track:, timeout:, deadline:, collapse:, … (see the migration guide's sidekiq_options table for the full set)

Returns:

  • (Hash) —

    the merged, string-keyed options hash



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wurk/job/options.rb', line 42

def sidekiq_options(opts = {})
  stringified = opts.to_h { |key, val| [key.to_s, val.is_a?(::Hash) ? val.transform_keys(&:to_s) : val] }
  Wurk::JobUtil.validate_track!(stringified['track'], stringified) if stringified.key?('track')
  Wurk::JobUtil.validate_bounds!(stringified)
  merged = get_sidekiq_options.merge(stringified)
  # On the merged options rather than the new ones, and before the
  # assign: a subclass adding `collapse:` to a parent's `unique_for:`
  # has declared both, and only the merge can see it. Raising first
  # leaves the class holding the options it had.
  Wurk::Collapse.policy_for(merged)
  @sidekiq_options_hash = merged
end

#sidekiq_retries_exhausted(&block) ⇒ Object



72
73
74
# File 'lib/wurk/job/options.rb', line 72

def sidekiq_retries_exhausted(&block)
  @sidekiq_retries_exhausted_block = block
end

#sidekiq_retry_in(&block) ⇒ Object



68
69
70
# File 'lib/wurk/job/options.rb', line 68

def sidekiq_retry_in(&block)
  @sidekiq_retry_in_block = block
end