Class: Sidekiq::Client

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

Class Method Summary collapse

Class Method Details

.default_middlewareObject



8
9
10
11
# File 'lib/sidekiq/client.rb', line 8

def self.default_middleware
  Middleware::Chain.new do |m|
  end
end

.enqueue(klass, *args) ⇒ Object

Resque compatibility helpers.

Example usage:

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

Messages are enqueued to the ‘default’ queue.



79
80
81
# File 'lib/sidekiq/client.rb', line 79

def self.enqueue(klass, *args)
  klass.client_push('class' => klass, 'args' => args)
end

.enqueue_to(queue, klass, *args) ⇒ Object

Example usage:

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


86
87
88
# File 'lib/sidekiq/client.rb', line 86

def self.enqueue_to(queue, klass, *args)
  klass.client_push('queue' => queue, 'class' => klass, 'args' => args)
end

.push(item) ⇒ Object

The main method used to push a job to Redis. Accepts a number of options:

queue - the named queue to use, default 'default'
class - the worker class to call, required
args - an array of simple arguments to the perform method, must be JSON-serializable
retry - whether to retry this job if it fails, true or false, default true
backtrace - whether to save any error backtrace, default false

All options must be strings, not symbols. NB: because we are serializing to JSON, all symbols in ‘args’ will be converted to strings.

Returns nil if not pushed to Redis or a unique Job ID if pushed.

Example:

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


38
39
40
41
42
43
44
45
# File 'lib/sidekiq/client.rb', line 38

def self.push(item)
  normed = normalize_item(item)
  normed, payload = process_single(item['class'], normed)

  pushed = false
  pushed = raw_push(normed, payload) if normed
  pushed ? normed['jid'] : nil
end

.push_bulk(items) ⇒ Object

Push a large number of jobs to Redis. In practice this method is only useful if you are pushing tens of thousands of jobs or more. This method basically cuts down on the redis round trip latency.

Takes the same arguments as Client.push except that args is expected to be an Array of Arrays. All other keys are duplicated for each job. Each job is run through the client middleware pipeline and each job gets its own Job ID as normal.

Returns the number of jobs pushed or nil if the pushed failed. The number of jobs pushed can be less than the number given if the middleware stopped processing for one or more jobs.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sidekiq/client.rb', line 60

def self.push_bulk(items)
  normed = normalize_item(items)
  payloads = items['args'].map do |args|
    _, payload = process_single(items['class'], normed.merge('args' => args, 'jid' => SecureRandom.hex(12)))
    payload
  end.compact

  pushed = false
  pushed = raw_push(normed, payloads)
  pushed ? payloads.size : nil
end

.registered_queuesObject



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

def self.registered_queues
  Sidekiq.redis { |x| x.smembers('queues') }
end

.registered_workersObject



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

def self.registered_workers
  Sidekiq.redis { |x| x.smembers('workers') }
end