1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/client.rb

87 lines
2.9 KiB
Ruby
Raw Normal View History

2012-08-23 13:03:50 -04:00
require 'securerandom'
require 'sidekiq/middleware/chain'
2012-01-21 19:42:21 -05:00
module Sidekiq
class Client
def self.default_middleware
Middleware::Chain.new do |m|
end
end
def self.registered_workers
Sidekiq.redis { |x| x.smembers('workers') }
end
def self.registered_queues
Sidekiq.redis { |x| x.smembers('queues') }
end
##
# 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.
#
2012-08-11 14:47:25 -04:00
# 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'])
#
def self.push(item)
raise(ArgumentError, "Message must be a Hash of the form: { 'class' => SomeWorker, 'args' => ['bob', 1, :foo => 'bar'] }") unless item.is_a?(Hash)
2012-01-23 15:56:49 -05:00
raise(ArgumentError, "Message must include a class and set of arguments: #{item.inspect}") if !item['class'] || !item['args']
raise(ArgumentError, "Message must include a Sidekiq::Worker class, not class name: #{item['class'].ancestors.inspect}") if !item['class'].is_a?(Class) || !item['class'].respond_to?('get_sidekiq_options')
2012-01-21 19:42:21 -05:00
worker_class = item['class']
item['class'] = item['class'].to_s
2012-04-29 16:22:39 -04:00
item = worker_class.get_sidekiq_options.merge(item)
item['retry'] = !!item['retry']
queue = item['queue']
item['jid'] = SecureRandom.hex(12)
pushed = false
Sidekiq.client_middleware.invoke(worker_class, item, queue) do
payload = Sidekiq.dump_json(item)
Sidekiq.redis do |conn|
if item['at']
pushed = conn.zadd('schedule', item['at'].to_s, payload)
else
_, pushed = conn.multi do
conn.sadd('queues', queue)
conn.rpush("queue:#{queue}", payload)
end
end
end
end
2012-08-11 14:47:25 -04:00
pushed ? item['jid'] : nil
2012-01-21 19:42:21 -05:00
end
# Resque compatibility helpers.
2012-01-21 19:42:21 -05:00
#
# Example usage:
# Sidekiq::Client.enqueue(MyWorker, 'foo', 1, :bat => 'bar')
2012-01-21 19:42:21 -05:00
#
# Messages are enqueued to the 'default' queue.
2012-01-21 19:42:21 -05:00
#
def self.enqueue(klass, *args)
klass.perform_async(*args)
2012-01-21 19:42:21 -05:00
end
# Example usage:
# Sidekiq::Client.enqueue_to(:queue_name, MyWorker, 'foo', 1, :bat => 'bar')
#
def self.enqueue_to(queue, klass, *args)
klass.perform_in_queue(queue, *args)
end
2012-01-21 19:42:21 -05:00
end
end