Module: Sidekiq::Worker::Options::ClassMethods

Defined in:
lib/sidekiq/worker.rb

Constant Summary collapse

ACCESSOR_MUTEX =
Mutex.new

Instance Method Summary collapse

Instance Method Details

#get_sidekiq_optionsObject

:nodoc:


63
64
65
# File 'lib/sidekiq/worker.rb', line 63

def get_sidekiq_options # :nodoc:
  self.sidekiq_options_hash ||= Sidekiq.default_worker_options
end

#sidekiq_class_attribute(*attrs) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/sidekiq/worker.rb', line 67

def sidekiq_class_attribute(*attrs)
  instance_reader = true
  instance_writer = true

  attrs.each do |name|
    synchronized_getter = "__synchronized_#{name}"

    singleton_class.instance_eval do
      undef_method(name) if method_defined?(name) || private_method_defined?(name)
    end

    define_singleton_method(synchronized_getter) { nil }
    singleton_class.class_eval do
      private(synchronized_getter)
    end

    define_singleton_method(name) { ACCESSOR_MUTEX.synchronize { send synchronized_getter } }

    ivar = "@#{name}"

    singleton_class.instance_eval do
      m = "#{name}="
      undef_method(m) if method_defined?(m) || private_method_defined?(m)
    end
    define_singleton_method("#{name}=") do |val|
      singleton_class.class_eval do
        ACCESSOR_MUTEX.synchronize do
          undef_method(synchronized_getter) if method_defined?(synchronized_getter) || private_method_defined?(synchronized_getter)
          define_method(synchronized_getter) { val }
        end
      end

      if singleton_class?
        class_eval do
          undef_method(name) if method_defined?(name) || private_method_defined?(name)
          define_method(name) do
            if instance_variable_defined? ivar
              instance_variable_get ivar
            else
              singleton_class.send name
            end
          end
        end
      end
      val
    end

    if instance_reader
      undef_method(name) if method_defined?(name) || private_method_defined?(name)
      define_method(name) do
        if instance_variable_defined?(ivar)
          instance_variable_get ivar
        else
          self.class.public_send name
        end
      end
    end

    if instance_writer
      m = "#{name}="
      undef_method(m) if method_defined?(m) || private_method_defined?(m)
      attr_writer name
    end
  end
end

#sidekiq_options(opts = {}) ⇒ Object

Allows customization for this type of Worker. Legal options:

queue - name of queue to use for this job type, default *default*
retry - enable retries for this Worker in case of error during execution,
   *true* to use the default or *Integer* count
backtrace - whether to save any error backtrace in the retry payload to display in web UI,
   can be true, false or an integer number of lines to save, default *false*

In practice, any option is allowed. This is the main mechanism to configure the options for a specific job.


50
51
52
53
# File 'lib/sidekiq/worker.rb', line 50

def sidekiq_options(opts = {})
  opts = Hash[opts.map { |k, v| [k.to_s, v] }] # stringify
  self.sidekiq_options_hash = get_sidekiq_options.merge(Hash[opts.map { |k, v| [k.to_s, v] }])
end

#sidekiq_retries_exhausted(&block) ⇒ Object


59
60
61
# File 'lib/sidekiq/worker.rb', line 59

def sidekiq_retries_exhausted(&block)
  self.sidekiq_retries_exhausted_block = block
end

#sidekiq_retry_in(&block) ⇒ Object


55
56
57
# File 'lib/sidekiq/worker.rb', line 55

def sidekiq_retry_in(&block)
  self.sidekiq_retry_in_block = block
end