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, Options Classes: Setter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jidObject

Returns the value of attribute jid


135
136
137
# File 'lib/sidekiq/worker.rb', line 135

def jid
  @jid
end

Class Method Details

.clear_allObject

Clear all queued jobs across all workers


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

def clear_all
  Queues.clear_all
end

.drain_allObject

Drain all queued jobs across all workers


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

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)

137
138
139
140
141
142
# File 'lib/sidekiq/worker.rb', line 137

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

  base.include(Options)
  base.extend(ClassMethods)
end

.jobsObject

:nodoc:


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

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

Instance Method Details

#loggerObject


144
145
146
# File 'lib/sidekiq/worker.rb', line 144

def logger
  Sidekiq.logger
end