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


44
45
46
47
# File 'lib/sidekiq/worker.rb', line 44

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

Instance Method Details

#perform_async(*args) ⇒ Object


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

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).

55
56
57
58
59
60
61
62
63
64
# File 'lib/sidekiq/worker.rb', line 55

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, 'at' => ts)  # Optimization to enqueue something now that is scheduled to go out now or in the past

  payload.delete('at') if ts <= now
  @klass.client_push(payload)
end