Module: Sidekiq::Worker::ClassMethods

Defined in:
lib/sidekiq/testing.rb,
lib/sidekiq/worker.rb,
lib/sidekiq/testing/inline.rb

Overview

The Sidekiq inline infrastructure overrides the perform_async so that it actually calls perform instead. This allows workers to be run inline in a testing environment.

This is similar to ‘Resque.inline = true` functionality.

Example:

require 'sidekiq/testing/inline'

$external_variable = 0

class ExternalWorker
  include Sidekiq::Worker

  def perform
    $external_variable = 1
  end
end

assert_equal 0, $external_variable
ExternalWorker.perform_async
assert_equal 1, $external_variable

Constant Summary collapse

DEFAULT_OPTIONS =
{ 'retry' => true, 'queue' => 'default' }

Instance Method Summary collapse

Instance Method Details

#client_push(opts) ⇒ Object

:nodoc:



71
72
73
# File 'lib/sidekiq/worker.rb', line 71

def client_push(*args) # :nodoc:
  Sidekiq::Client.push(*args)
end

#client_push_oldObject

:nodoc:



27
28
29
# File 'lib/sidekiq/testing.rb', line 27

def client_push(*args) # :nodoc:
  Sidekiq::Client.push(*args)
end

#drainObject



37
38
39
40
41
# File 'lib/sidekiq/testing.rb', line 37

def drain
  while job = jobs.shift do
    new.perform(*job['args'])
  end
end

#get_sidekiq_optionsObject

:nodoc:



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

def get_sidekiq_options # :nodoc:
  self.sidekiq_options_hash ||= DEFAULT_OPTIONS
end

#jobsObject



33
34
35
# File 'lib/sidekiq/testing.rb', line 33

def jobs
  @pushed ||= []
end

#perform_async(*args) ⇒ Object



34
35
36
# File 'lib/sidekiq/worker.rb', line 34

def perform_async(*args)
  client_push('class' => self, 'args' => args)
end

#perform_async_oldObject



30
31
32
# File 'lib/sidekiq/testing/inline.rb', line 30

def perform_async(*args)
  client_push('class' => self, 'args' => args)
end

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



38
39
40
41
42
# File 'lib/sidekiq/worker.rb', line 38

def perform_in(interval, *args)
  int = interval.to_f
  ts = (int < 1_000_000_000 ? Time.now.to_f + int : int)
  client_push('class' => self, 'args' => args, 'at' => ts)
end

#sidekiq_options(opts = {}) ⇒ Object

Allows customization for this type of Worker. Legal options:

:queue - use a named queue for this Worker, default 'default'
:retry - enable the RetryJobs middleware for this Worker, default *true*
:timeout - timeout the perform method after N seconds, default *nil*
: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*


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

def sidekiq_options(opts={})
  self.sidekiq_options_hash = get_sidekiq_options.merge(stringify_keys(opts || {}))
end

#stringify_keys(hash) ⇒ Object

:nodoc:



64
65
66
67
68
69
# File 'lib/sidekiq/worker.rb', line 64

def stringify_keys(hash) # :nodoc:
  hash.keys.each do |key|
    hash[key.to_s] = hash.delete(key)
  end
  hash
end