2012-08-23 13:03:50 -04:00
require 'securerandom'
2012-02-08 20:04:02 -05:00
require 'sidekiq/middleware/chain'
2012-01-21 19:42:21 -05:00
module Sidekiq
class Client
2012-02-10 23:20:01 -05:00
2012-02-19 16:02:32 -05:00
def self . default_middleware
Middleware :: Chain . new do | m |
end
end
2012-02-15 14:28:19 -05:00
def self . registered_workers
2012-03-14 12:56:13 -04:00
Sidekiq . redis { | x | x . smembers ( 'workers' ) }
2012-02-15 14:28:19 -05:00
end
def self . registered_queues
2012-03-14 12:56:13 -04:00
Sidekiq . redis { | x | x . smembers ( 'queues' ) }
2012-02-15 14:28:19 -05:00
end
2012-04-19 17:42:17 -04:00
##
# 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
2012-04-27 23:25:46 -04:00
# backtrace - whether to save any error backtrace, default false
2012-04-19 17:42:17 -04:00
#
# 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.
#
2012-04-19 17:42:17 -04:00
# Example:
# Sidekiq::Client.push('queue' => 'my_queue', 'class' => MyWorker, 'args' => ['foo', 1, :bat => 'bar'])
#
2012-04-01 22:53:45 -04:00
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' ]
2012-04-01 22:53:45 -04:00
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
2012-04-01 22:53:45 -04:00
worker_class = item [ 'class' ]
item [ 'class' ] = item [ 'class' ] . to_s
2012-04-26 11:40:07 -04:00
2012-04-29 16:22:39 -04:00
item = worker_class . get_sidekiq_options . merge ( item )
item [ 'retry' ] = ! ! item [ 'retry' ]
queue = item [ 'queue' ]
2012-09-05 00:33:06 -04:00
item [ 'jid' ] = SecureRandom . hex ( 12 )
2012-02-10 23:20:01 -05:00
pushed = false
2012-04-01 22:53:45 -04:00
Sidekiq . client_middleware . invoke ( worker_class , item , queue ) do
2012-04-22 17:02:35 -04:00
payload = Sidekiq . dump_json ( item )
2012-03-14 12:56:13 -04:00
Sidekiq . redis do | conn |
2012-05-25 23:21:42 -04:00
if item [ 'at' ]
2012-06-20 18:24:23 -04:00
pushed = conn . zadd ( 'schedule' , item [ 'at' ] . to_s , payload )
2012-05-25 23:21:42 -04:00
else
_ , pushed = conn . multi do
conn . sadd ( 'queues' , queue )
conn . rpush ( " queue: #{ queue } " , payload )
end
2012-03-10 16:07:19 -05:00
end
end
2012-02-07 06:29:09 -05:00
end
2012-08-11 14:47:25 -04:00
pushed ? item [ 'jid' ] : nil
2012-01-21 19:42:21 -05:00
end
2012-09-07 11:35:27 -04:00
# Resque compatibility helpers.
2012-01-21 19:42:21 -05:00
#
2012-09-07 11:35:27 -04:00
# Example usage:
# Sidekiq::Client.enqueue(MyWorker, 'foo', 1, :bat => 'bar')
2012-01-21 19:42:21 -05:00
#
2012-02-09 23:32:59 -05:00
# Messages are enqueued to the 'default' queue.
2012-01-21 19:42:21 -05:00
#
def self . enqueue ( klass , * args )
2012-09-07 11:35:27 -04:00
klass . client_push ( 'class' = > self , 'args' = > args )
2012-01-21 19:42:21 -05:00
end
2012-07-17 10:48:54 -04:00
2012-09-07 11:35:27 -04:00
# Example usage:
# Sidekiq::Client.enqueue_to(:queue_name, MyWorker, 'foo', 1, :bat => 'bar')
2012-07-17 10:48:54 -04:00
#
def self . enqueue_to ( queue , klass , * args )
2012-09-07 11:35:27 -04:00
klass . client_push ( 'queue' = > queue , 'class' = > self , 'args' = > args )
2012-07-17 10:48:54 -04:00
end
2012-01-21 19:42:21 -05:00
end
end