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

Instance Method Summary collapse

Instance Method Details

#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:



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

def get_sidekiq_options # :nodoc:
  defined?(@sidekiq_options) ? @sidekiq_options : { 'unique' => true, 'retry' => true, 'queue' => 'default' }
end

#jobsObject



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

def jobs
  @pushed ||= []
end

#perform_async(*args) ⇒ Object



32
33
34
# File 'lib/sidekiq/worker.rb', line 32

def perform_async(*args)
  Sidekiq::Client.push('class' => self, 'args' => args)
end

#perform_async_oldObject



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

def perform_async(*args)
  Sidekiq::Client.push('class' => self, 'args' => args)
end

#sidekiq_options(opts = {}) ⇒ Object

Allows customization for this type of Worker. Legal options:

:unique - enable the UniqueJobs middleware for this Worker, default *true*
: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*


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

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

#stringify_keys(hash) ⇒ Object

:nodoc:



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

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