Class: Sidekiq::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/client.rb

Class Method Summary collapse

Class Method Details

.default_middlewareObject


16
17
18
19
20
21
# File 'lib/sidekiq/client.rb', line 16

def self.default_middleware
  Middleware::Chain.new do |m|
    m.add Middleware::Client::UniqueJobs
    m.add Middleware::Client::ResqueWebCompatibility
  end
end

.enqueue(klass, *args) ⇒ Object

Please use .push if possible instead.

Example usage:

Sidekiq::Client.enqueue(MyWorker, 'foo', 1, :bat => 'bar')

Messages are enqueued to the 'default' queue.


61
62
63
# File 'lib/sidekiq/client.rb', line 61

def self.enqueue(klass, *args)
  push(nil, { 'class' => klass.name, 'args' => args })
end

.middlewareObject


12
13
14
# File 'lib/sidekiq/client.rb', line 12

def self.middleware
  raise "Sidekiq::Client.middleware is now Sidekiq.client_middleware"
end

.push(queue = nil, item) ⇒ Object

Example usage: Sidekiq::Client.push('my_queue', 'class' => MyWorker, 'args' => ['foo', 1, :bat => 'bar'])

Raises:

  • (ArgumentError)

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sidekiq/client.rb', line 37

def self.push(queue=nil, item)
  raise(ArgumentError, "Message must be a Hash of the form: { 'class' => SomeClass, 'args' => ['bob', 1, :foo => 'bar'] }") unless item.is_a?(Hash)
  raise(ArgumentError, "Message must include a class and set of arguments: #{item.inspect}") if !item['class'] || !item['args']

  queue = queue || queue_mappings[item['class'].to_s] || 'default'

  item['class'] = item['class'].to_s if !item['class'].is_a?(String)

  pushed = false
  Sidekiq.client_middleware.invoke(item, queue) do
    Sidekiq.redis.rpush("queue:#{queue}", MultiJson.encode(item))
    pushed = true
  end
  pushed
end

.queue_mappingsObject


31
32
33
# File 'lib/sidekiq/client.rb', line 31

def self.queue_mappings
  @queue_mappings ||= {}
end

.registered_queuesObject


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

def self.registered_queues
  Sidekiq.redis.smembers('queues')
end

.registered_workersObject


23
24
25
# File 'lib/sidekiq/client.rb', line 23

def self.registered_workers
  Sidekiq.redis.smembers('workers')
end