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
- #drain ⇒ Object
-
#get_sidekiq_options ⇒ Object
:nodoc:.
- #jobs ⇒ Object
- #perform_async(*args) ⇒ Object
- #perform_async_old ⇒ Object
- #perform_in(interval, *args) ⇒ Object (also: #perform_at)
- #perform_in_old ⇒ Object
-
#sidekiq_options(opts = {}) ⇒ Object
Allows customization for this type of Worker.
-
#stringify_keys(hash) ⇒ Object
:nodoc:.
Instance Method Details
#drain ⇒ Object
47 48 49 50 51 |
# File 'lib/sidekiq/testing.rb', line 47 def drain while job = jobs.shift do new.perform(*job['args']) end end |
#get_sidekiq_options ⇒ Object
:nodoc:
57 58 59 |
# File 'lib/sidekiq/worker.rb', line 57 def # :nodoc: defined?(@sidekiq_options) ? @sidekiq_options : { 'unique' => true, 'retry' => true, 'queue' => 'default' } end |
#jobs ⇒ Object
43 44 45 |
# File 'lib/sidekiq/testing.rb', line 43 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_old ⇒ Object
27 28 29 |
# File 'lib/sidekiq/testing.rb', line 27 def perform_async(*args) Sidekiq::Client.push('class' => self, 'args' => args) end |
#perform_in(interval, *args) ⇒ Object Also known as: perform_at
36 37 38 39 40 |
# File 'lib/sidekiq/worker.rb', line 36 def perform_in(interval, *args) int = interval.to_f ts = (int < 1_000_000_000 ? Time.now.to_f + int : int) Sidekiq::Client.push('class' => self, 'args' => args, 'at' => ts) end |
#perform_in_old ⇒ Object
33 34 35 36 37 |
# File 'lib/sidekiq/testing.rb', line 33 def perform_in(interval, *args) int = interval.to_f ts = (int < 1_000_000_000 ? Time.now.to_f + int : int) Sidekiq::Client.push('class' => self, 'args' => args, 'at' => ts) 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*
53 54 55 |
# File 'lib/sidekiq/worker.rb', line 53 def (opts={}) @sidekiq_options = .merge(stringify_keys(opts || {})) end |
#stringify_keys(hash) ⇒ Object
:nodoc:
61 62 63 64 65 66 |
# File 'lib/sidekiq/worker.rb', line 61 def stringify_keys(hash) # :nodoc: hash.keys.each do |key| hash[key.to_s] = hash.delete(key) end hash end |