Class: Sidekiq::Worker::Setter

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/worker.rb

Overview

This helper class encapsulates the set options for ‘set`, e.g.

SomeWorker.set(queue: 'foo').perform_async(....)

Instance Method Summary collapse

Constructor Details

#initialize(klass, opts) ⇒ Setter

Returns a new instance of Setter.



153
154
155
156
# File 'lib/sidekiq/worker.rb', line 153

def initialize(klass, opts)
  @klass = klass
  @opts = opts
end

Instance Method Details

#perform_async(*args) ⇒ Object



163
164
165
# File 'lib/sidekiq/worker.rb', line 163

def perform_async(*args)
  @klass.client_push(@opts.merge("args" => args, "class" => @klass))
end

#perform_in(interval, *args) ⇒ Object Also known as: perform_at

interval must be a timestamp, numeric or something that acts

numeric (like an activesupport time interval).


169
170
171
172
173
174
175
176
177
178
# File 'lib/sidekiq/worker.rb', line 169

def perform_in(interval, *args)
  int = interval.to_f
  now = Time.now.to_f
  ts = (int < 1_000_000_000 ? now + int : int)

  payload = @opts.merge("class" => @klass, "args" => args)
  # Optimization to enqueue something now that is scheduled to go out now or in the past
  payload["at"] = ts if ts > now
  @klass.client_push(payload)
end

#set(options) ⇒ Object



158
159
160
161
# File 'lib/sidekiq/worker.rb', line 158

def set(options)
  @opts.merge!(options)
  self
end