Module: Sidekiq::Worker

Included in:
Extensions::DelayedClass, Extensions::DelayedMailer, Extensions::DelayedModel
Defined in:
lib/sidekiq/worker.rb,
lib/sidekiq/testing.rb

Overview

Include this module in your worker class and you can easily create asynchronous jobs:

class HardWorker

include Sidekiq::Worker

def perform(*args)
  # do some work
end

end

Then in your Rails app, you can do this:

HardWorker.perform_async(1, 2, 3)

Note that perform_async is a class method, perform is an instance method.

Defined Under Namespace

Modules: ClassMethods Classes: Setter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jidObject

Returns the value of attribute jid



24
25
26
# File 'lib/sidekiq/worker.rb', line 24

def jid
  @jid
end

Class Method Details

.clear_allObject

Clear all queued jobs across all workers



311
312
313
# File 'lib/sidekiq/testing.rb', line 311

def clear_all
  Queues.clear_all
end

.drain_allObject

Drain all queued jobs across all workers



316
317
318
319
320
321
322
323
324
# File 'lib/sidekiq/testing.rb', line 316

def drain_all
  while jobs.any?
    worker_classes = jobs.map { |job| job["class"] }.uniq

    worker_classes.each do |worker_class|
      Sidekiq::Testing.constantize(worker_class).drain
    end
  end
end

.included(base) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
# File 'lib/sidekiq/worker.rb', line 26

def self.included(base)
  raise ArgumentError, "You cannot include Sidekiq::Worker in an ActiveJob: #{base.name}" if base.ancestors.any? {|c| c.name == 'ActiveJob::Base' }

  base.extend(ClassMethods)
  base.sidekiq_class_attribute :sidekiq_options_hash
  base.sidekiq_class_attribute :sidekiq_retry_in_block
  base.sidekiq_class_attribute :sidekiq_retries_exhausted_block
end

.jobsObject

:nodoc:



306
307
308
# File 'lib/sidekiq/testing.rb', line 306

def jobs # :nodoc:
  Queues.jobs_by_queue.values.flatten
end

Instance Method Details

#loggerObject



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

def logger
  Sidekiq.logger
end