class Sidekiq::Worker::Setter

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

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

Public Class Methods

new(klass, opts) click to toggle source
# File lib/sidekiq/worker.rb, line 174
def initialize(klass, opts)
  @klass = klass
  @opts = opts

  # ActiveJob compatibility
  interval = @opts.delete(:wait_until) || @opts.delete(:wait)
  at(interval) if interval
end

Public Instance Methods

perform_async(*args) click to toggle source
# File lib/sidekiq/worker.rb, line 190
def perform_async(*args)
  @klass.client_push(@opts.merge("args" => args, "class" => @klass))
end
perform_at(interval, *args)
Alias for: perform_in
perform_bulk(args, batch_size: 1_000) click to toggle source
# File lib/sidekiq/worker.rb, line 194
def perform_bulk(args, batch_size: 1_000)
  args.each_slice(batch_size).flat_map do |slice|
    Sidekiq::Client.push_bulk(@opts.merge("class" => @klass, "args" => slice))
  end
end
perform_in(interval, *args) click to toggle source

interval must be a timestamp, numeric or something that acts

numeric (like an activesupport time interval).
# File lib/sidekiq/worker.rb, line 202
def perform_in(interval, *args)
  at(interval).perform_async(*args)
end
Also aliased as: perform_at
set(options) click to toggle source
# File lib/sidekiq/worker.rb, line 183
def set(options)
  interval = options.delete(:wait_until) || options.delete(:wait)
  @opts.merge!(options)
  at(interval) if interval
  self
end

Private Instance Methods

at(interval) click to toggle source
# File lib/sidekiq/worker.rb, line 209
def at(interval)
  int = interval.to_f
  now = Time.now.to_f
  ts = (int < 1_000_000_000 ? now + int : int)
  # Optimization to enqueue something now that is scheduled to go out now or in the past
  @opts["at"] = ts if ts > now
  self
end