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

59 lines
1.6 KiB
Ruby
Raw Normal View History

2012-01-21 19:42:21 -05:00
require 'multi_json'
2012-01-22 14:32:38 -05:00
require 'redis'
2012-01-21 19:42:21 -05:00
require 'sidekiq/redis_connection'
require 'sidekiq/middleware/chain'
require 'sidekiq/middleware/client/resque_web_compatibility'
require 'sidekiq/middleware/client/unique_jobs'
2012-01-21 19:42:21 -05:00
module Sidekiq
class Client
def self.middleware
raise "Sidekiq::Client.middleware is now Sidekiq.client_middleware"
end
def self.registered_workers
2012-02-17 16:39:36 -05:00
Sidekiq.redis.smembers('workers')
end
def self.registered_queues
2012-02-17 16:39:36 -05:00
Sidekiq.redis.smembers('queues')
end
def self.queue_mappings
@queue_mappings ||= {}
end
2012-01-21 19:42:21 -05:00
# Example usage:
2012-01-23 15:56:49 -05:00
# Sidekiq::Client.push('my_queue', 'class' => MyWorker, 'args' => ['foo', 1, :bat => 'bar'])
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)
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-01-21 19:42:21 -05:00
queue = queue || queue_mappings[item['class'].to_s] || 'default'
2012-01-23 15:56:49 -05:00
item['class'] = item['class'].to_s if !item['class'].is_a?(String)
pushed = false
Sidekiq.client_middleware.invoke(item, queue) do
2012-02-17 16:39:36 -05:00
Sidekiq.redis.rpush("queue:#{queue}", MultiJson.encode(item))
pushed = true
end
pushed
2012-01-21 19:42:21 -05:00
end
# Please use .push if possible instead.
#
# Example usage:
#
# Sidekiq::Client.enqueue(MyWorker, 'foo', 1, :bat => 'bar')
#
# Messages are enqueued to the 'default' queue.
2012-01-21 19:42:21 -05:00
#
def self.enqueue(klass, *args)
push(nil, { 'class' => klass.name, 'args' => args })
2012-01-21 19:42:21 -05:00
end
end
end