2012-01-22 16:01:46 -08:00
|
|
|
require 'sidekiq/version'
|
|
|
|
require 'sidekiq/client'
|
2012-01-25 13:32:51 -08:00
|
|
|
require 'sidekiq/worker'
|
2012-03-10 12:30:15 -08:00
|
|
|
require 'sidekiq/rails'
|
2012-02-18 12:12:05 -08:00
|
|
|
require 'sidekiq/redis_connection'
|
2012-03-17 09:41:24 -07:00
|
|
|
require 'sidekiq/util'
|
2012-02-17 13:39:36 -08:00
|
|
|
|
2012-03-10 12:30:15 -08:00
|
|
|
require 'sidekiq/extensions/action_mailer'
|
|
|
|
require 'sidekiq/extensions/active_record'
|
2012-02-18 12:12:05 -08:00
|
|
|
|
2012-02-17 13:39:36 -08:00
|
|
|
module Sidekiq
|
2012-02-18 12:12:05 -08:00
|
|
|
|
2012-02-19 13:02:32 -08:00
|
|
|
DEFAULTS = {
|
|
|
|
:queues => [],
|
|
|
|
:concurrency => 25,
|
|
|
|
:require => '.',
|
|
|
|
:environment => nil,
|
2012-04-06 20:53:03 -07:00
|
|
|
:timeout => 8,
|
2012-03-26 18:05:33 -05:00
|
|
|
:enable_rails_extensions => true,
|
2012-02-19 13:02:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
def self.options
|
|
|
|
@options ||= DEFAULTS.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.options=(opts)
|
|
|
|
@options = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2012-02-20 09:46:28 -08:00
|
|
|
# Configuration for Sidekiq server, use like:
|
2012-02-19 13:02:32 -08:00
|
|
|
#
|
2012-02-20 09:46:28 -08:00
|
|
|
# Sidekiq.configure_server do |config|
|
2012-03-06 13:42:30 -08:00
|
|
|
# config.redis = { :namespace => 'myapp', :size => 25, :url => 'redis://myhost:8877/mydb' }
|
2012-02-19 13:02:32 -08:00
|
|
|
# config.server_middleware do |chain|
|
|
|
|
# chain.add MyServerHook
|
|
|
|
# end
|
|
|
|
# end
|
2012-02-20 09:46:28 -08:00
|
|
|
def self.configure_server
|
|
|
|
yield self if server?
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Configuration for Sidekiq client, use like:
|
|
|
|
#
|
|
|
|
# Sidekiq.configure_client do |config|
|
2012-03-06 13:42:30 -08:00
|
|
|
# config.redis = { :namespace => 'myapp', :size => 1, :url => 'redis://myhost:8877/mydb' }
|
2012-02-20 09:46:28 -08:00
|
|
|
# end
|
|
|
|
def self.configure_client
|
|
|
|
yield self unless server?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.server?
|
|
|
|
defined?(Sidekiq::CLI)
|
2012-02-19 13:02:32 -08:00
|
|
|
end
|
|
|
|
|
2012-03-14 09:56:13 -07:00
|
|
|
def self.redis(&block)
|
2012-02-17 13:51:46 -08:00
|
|
|
@redis ||= Sidekiq::RedisConnection.create
|
2012-03-28 19:16:54 -07:00
|
|
|
raise ArgumentError, "requires a block" if !block
|
|
|
|
@redis.with(&block)
|
2012-02-17 13:51:46 -08:00
|
|
|
end
|
2012-02-18 12:12:05 -08:00
|
|
|
|
2012-03-01 13:41:12 -08:00
|
|
|
def self.redis=(hash)
|
2012-03-28 19:16:54 -07:00
|
|
|
if hash.is_a?(Hash)
|
|
|
|
@redis = RedisConnection.create(hash)
|
2012-04-09 08:56:34 -07:00
|
|
|
elsif hash.is_a?(ConnectionPool)
|
2012-03-28 19:16:54 -07:00
|
|
|
@redis = hash
|
2012-04-09 08:56:34 -07:00
|
|
|
else
|
|
|
|
raise ArgumentError, "redis= requires a Hash or ConnectionPool"
|
2012-03-01 13:41:12 -08:00
|
|
|
end
|
2012-02-17 13:39:36 -08:00
|
|
|
end
|
2012-02-18 12:12:05 -08:00
|
|
|
|
|
|
|
def self.client_middleware
|
2012-02-19 13:02:32 -08:00
|
|
|
@client_chain ||= Client.default_middleware
|
2012-02-18 12:12:05 -08:00
|
|
|
yield @client_chain if block_given?
|
|
|
|
@client_chain
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.server_middleware
|
2012-02-19 13:02:32 -08:00
|
|
|
@server_chain ||= Processor.default_middleware
|
2012-02-18 12:12:05 -08:00
|
|
|
yield @server_chain if block_given?
|
|
|
|
@server_chain
|
|
|
|
end
|
|
|
|
|
2012-04-22 19:22:09 -07:00
|
|
|
def self.load_json(string)
|
2012-04-22 14:02:35 -07:00
|
|
|
# Can't reliably detect whether MultiJson responds to load, since it's
|
|
|
|
# a reserved word. Use adapter as a proxy for new features.
|
|
|
|
if MultiJson.respond_to?(:adapter)
|
2012-04-22 19:22:09 -07:00
|
|
|
MultiJson.load(string)
|
2012-04-22 14:02:35 -07:00
|
|
|
else
|
2012-04-22 19:22:09 -07:00
|
|
|
MultiJson.decode(string)
|
2012-04-22 14:02:35 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-22 19:22:09 -07:00
|
|
|
def self.dump_json(object)
|
2012-04-22 14:02:35 -07:00
|
|
|
if MultiJson.respond_to?(:dump)
|
2012-04-22 19:22:09 -07:00
|
|
|
MultiJson.dump(object)
|
2012-04-22 14:02:35 -07:00
|
|
|
else
|
2012-04-22 19:22:09 -07:00
|
|
|
MultiJson.encode(object)
|
2012-04-22 14:02:35 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-17 13:39:36 -08:00
|
|
|
end
|