2013-02-08 16:10:21 -05:00
|
|
|
|
# encoding: utf-8
|
2012-01-22 19:01:46 -05:00
|
|
|
|
require 'sidekiq/version'
|
2012-05-15 22:44:35 -04:00
|
|
|
|
require 'sidekiq/logging'
|
2012-01-22 19:01:46 -05:00
|
|
|
|
require 'sidekiq/client'
|
2012-01-25 16:32:51 -05:00
|
|
|
|
require 'sidekiq/worker'
|
2012-02-18 15:12:05 -05:00
|
|
|
|
require 'sidekiq/redis_connection'
|
2012-03-17 12:41:24 -04:00
|
|
|
|
require 'sidekiq/util'
|
2012-10-20 17:03:43 -04:00
|
|
|
|
require 'sidekiq/api'
|
2012-02-17 16:39:36 -05:00
|
|
|
|
|
2012-08-16 19:43:01 -04:00
|
|
|
|
require 'sidekiq/extensions/class_methods'
|
2012-03-10 15:30:15 -05:00
|
|
|
|
require 'sidekiq/extensions/action_mailer'
|
|
|
|
|
require 'sidekiq/extensions/active_record'
|
2012-06-13 18:38:12 -04:00
|
|
|
|
require 'sidekiq/rails' if defined?(::Rails::Engine)
|
2012-02-18 15:12:05 -05:00
|
|
|
|
|
2012-05-25 23:21:42 -04:00
|
|
|
|
require 'multi_json'
|
|
|
|
|
|
2012-02-17 16:39:36 -05:00
|
|
|
|
module Sidekiq
|
2012-09-09 15:41:23 -04:00
|
|
|
|
NAME = "Sidekiq"
|
2012-09-03 14:34:07 -04:00
|
|
|
|
LICENSE = 'See LICENSE and the LGPL-3.0 for licensing details.'
|
2012-02-18 15:12:05 -05:00
|
|
|
|
|
2012-02-19 16:02:32 -05:00
|
|
|
|
DEFAULTS = {
|
|
|
|
|
:queues => [],
|
|
|
|
|
:concurrency => 25,
|
|
|
|
|
:require => '.',
|
|
|
|
|
:environment => nil,
|
2012-04-06 23:53:03 -04:00
|
|
|
|
:timeout => 8,
|
2013-01-27 17:03:40 -05:00
|
|
|
|
:profile => false,
|
2012-02-19 16:02:32 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-19 18:54:55 -04:00
|
|
|
|
def self.❨╯°□°❩╯︵┻━┻
|
2013-02-08 15:42:06 -05:00
|
|
|
|
puts "Calm down, bro"
|
|
|
|
|
end
|
|
|
|
|
|
2012-02-19 16:02:32 -05:00
|
|
|
|
def self.options
|
|
|
|
|
@options ||= DEFAULTS.dup
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.options=(opts)
|
|
|
|
|
@options = opts
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
2012-02-20 12:46:28 -05:00
|
|
|
|
# Configuration for Sidekiq server, use like:
|
2012-02-19 16:02:32 -05:00
|
|
|
|
#
|
2012-02-20 12:46:28 -05:00
|
|
|
|
# Sidekiq.configure_server do |config|
|
2012-03-06 16:42:30 -05:00
|
|
|
|
# config.redis = { :namespace => 'myapp', :size => 25, :url => 'redis://myhost:8877/mydb' }
|
2012-02-19 16:02:32 -05:00
|
|
|
|
# config.server_middleware do |chain|
|
|
|
|
|
# chain.add MyServerHook
|
|
|
|
|
# end
|
|
|
|
|
# end
|
2012-02-20 12:46:28 -05:00
|
|
|
|
def self.configure_server
|
|
|
|
|
yield self if server?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
# Configuration for Sidekiq client, use like:
|
|
|
|
|
#
|
|
|
|
|
# Sidekiq.configure_client do |config|
|
2012-03-06 16:42:30 -05:00
|
|
|
|
# config.redis = { :namespace => 'myapp', :size => 1, :url => 'redis://myhost:8877/mydb' }
|
2012-02-20 12:46:28 -05:00
|
|
|
|
# end
|
|
|
|
|
def self.configure_client
|
|
|
|
|
yield self unless server?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.server?
|
|
|
|
|
defined?(Sidekiq::CLI)
|
2012-02-19 16:02:32 -05:00
|
|
|
|
end
|
|
|
|
|
|
2012-03-14 12:56:13 -04:00
|
|
|
|
def self.redis(&block)
|
2012-03-28 22:16:54 -04:00
|
|
|
|
raise ArgumentError, "requires a block" if !block
|
2013-02-02 05:47:58 -05:00
|
|
|
|
@redis ||= Sidekiq::RedisConnection.create
|
2012-03-28 22:16:54 -04:00
|
|
|
|
@redis.with(&block)
|
2012-02-17 16:51:46 -05:00
|
|
|
|
end
|
2012-02-18 15:12:05 -05:00
|
|
|
|
|
2012-03-01 16:41:12 -05:00
|
|
|
|
def self.redis=(hash)
|
2012-03-28 22:16:54 -04:00
|
|
|
|
if hash.is_a?(Hash)
|
|
|
|
|
@redis = RedisConnection.create(hash)
|
2012-04-09 11:56:34 -04:00
|
|
|
|
elsif hash.is_a?(ConnectionPool)
|
2012-03-28 22:16:54 -04:00
|
|
|
|
@redis = hash
|
2012-04-09 11:56:34 -04:00
|
|
|
|
else
|
|
|
|
|
raise ArgumentError, "redis= requires a Hash or ConnectionPool"
|
2012-03-01 16:41:12 -05:00
|
|
|
|
end
|
2012-02-17 16:39:36 -05:00
|
|
|
|
end
|
2012-02-18 15:12:05 -05:00
|
|
|
|
|
|
|
|
|
def self.client_middleware
|
2012-02-19 16:02:32 -05:00
|
|
|
|
@client_chain ||= Client.default_middleware
|
2012-02-18 15:12:05 -05:00
|
|
|
|
yield @client_chain if block_given?
|
|
|
|
|
@client_chain
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.server_middleware
|
2012-02-19 16:02:32 -05:00
|
|
|
|
@server_chain ||= Processor.default_middleware
|
2012-02-18 15:12:05 -05:00
|
|
|
|
yield @server_chain if block_given?
|
|
|
|
|
@server_chain
|
|
|
|
|
end
|
|
|
|
|
|
2012-04-22 22:22:09 -04:00
|
|
|
|
def self.load_json(string)
|
2013-02-09 16:12:09 -05:00
|
|
|
|
MultiJson.decode(string, :symbolize_keys => false)
|
2012-04-22 17:02:35 -04:00
|
|
|
|
end
|
|
|
|
|
|
2012-04-22 22:22:09 -04:00
|
|
|
|
def self.dump_json(object)
|
2012-12-14 14:07:21 -05:00
|
|
|
|
MultiJson.encode(object)
|
2012-04-22 17:02:35 -04:00
|
|
|
|
end
|
|
|
|
|
|
2012-05-15 22:44:35 -04:00
|
|
|
|
def self.logger
|
|
|
|
|
Sidekiq::Logging.logger
|
|
|
|
|
end
|
|
|
|
|
|
2012-06-26 12:01:54 -04:00
|
|
|
|
def self.logger=(log)
|
|
|
|
|
Sidekiq::Logging.logger = log
|
|
|
|
|
end
|
|
|
|
|
|
2012-06-19 19:36:59 -04:00
|
|
|
|
def self.poll_interval=(interval)
|
|
|
|
|
self.options[:poll_interval] = interval
|
|
|
|
|
end
|
|
|
|
|
|
2012-12-29 22:56:20 -05:00
|
|
|
|
##
|
|
|
|
|
# deprecated
|
|
|
|
|
def self.size(*queues)
|
|
|
|
|
return Sidekiq::Stats.new.enqueued if queues.empty?
|
|
|
|
|
|
|
|
|
|
Sidekiq.redis { |conn|
|
|
|
|
|
conn.multi {
|
|
|
|
|
queues.map { |q| conn.llen("queue:#{q}") }
|
|
|
|
|
}
|
2013-03-22 00:46:46 -04:00
|
|
|
|
}.inject(0) { |memo, count| memo + count }
|
2012-12-29 22:56:20 -05:00
|
|
|
|
end
|
|
|
|
|
|
2012-02-17 16:39:36 -05:00
|
|
|
|
end
|