2013-02-08 16:10:21 -05:00
|
|
|
|
# encoding: utf-8
|
2012-01-22 19:01:46 -05:00
|
|
|
|
require 'sidekiq/version'
|
2014-07-10 09:49:21 -04:00
|
|
|
|
fail "Sidekiq #{Sidekiq::VERSION} does not support Ruby 1.9." if RUBY_PLATFORM != 'java' && RUBY_VERSION < '2.0.0'
|
|
|
|
|
|
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-02-17 16:39:36 -05:00
|
|
|
|
|
2013-05-12 17:33:49 -04:00
|
|
|
|
require 'json'
|
2012-05-25 23:21:42 -04:00
|
|
|
|
|
2012-02-17 16:39:36 -05:00
|
|
|
|
module Sidekiq
|
2014-12-17 15:08:43 -05: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 = {
|
2014-12-17 15:08:43 -05:00
|
|
|
|
queues: [],
|
|
|
|
|
labels: [],
|
|
|
|
|
concurrency: 25,
|
|
|
|
|
require: '.',
|
|
|
|
|
environment: nil,
|
|
|
|
|
timeout: 8,
|
|
|
|
|
error_handlers: [],
|
|
|
|
|
lifecycle_events: {
|
|
|
|
|
startup: [],
|
|
|
|
|
quiet: [],
|
|
|
|
|
shutdown: [],
|
2015-02-04 15:33:49 -05:00
|
|
|
|
},
|
|
|
|
|
dead_max_jobs: 10_000,
|
|
|
|
|
dead_timeout_in_seconds: 180 * 24 * 60 * 60 # 6 months
|
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|
|
2013-04-18 12:11:49 -04:00
|
|
|
|
# config.redis = { :namespace => 'myapp', :size => 25, :url => 'redis://myhost:8877/0' }
|
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|
|
2013-04-18 12:11:49 -04:00
|
|
|
|
# config.redis = { :namespace => 'myapp', :size => 1, :url => 'redis://myhost:8877/0' }
|
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)
|
2014-07-10 09:49:21 -04:00
|
|
|
|
raise ArgumentError, "requires a block" unless block
|
2014-03-24 13:56:31 -04:00
|
|
|
|
redis_pool.with(&block)
|
2014-03-24 13:20:16 -04:00
|
|
|
|
end
|
|
|
|
|
|
2014-03-24 13:56:31 -04:00
|
|
|
|
def self.redis_pool
|
2014-03-14 00:06:39 -04:00
|
|
|
|
@redis ||= Sidekiq::RedisConnection.create
|
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)
|
2014-03-14 00:06:39 -04:00
|
|
|
|
@redis = if hash.is_a?(ConnectionPool)
|
|
|
|
|
hash
|
2012-04-09 11:56:34 -04:00
|
|
|
|
else
|
2014-03-14 00:06:39 -04:00
|
|
|
|
Sidekiq::RedisConnection.create(hash)
|
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
|
|
|
|
|
2013-10-24 00:58:15 -04:00
|
|
|
|
def self.client_middleware
|
2013-10-24 00:47:57 -04:00
|
|
|
|
@client_chain ||= Middleware::Chain.new
|
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
|
|
|
|
|
|
2013-09-07 12:54:13 -04:00
|
|
|
|
def self.default_worker_options=(hash)
|
2015-01-09 13:26:21 -05:00
|
|
|
|
@default_worker_options = default_worker_options.merge(hash.stringify_keys)
|
2013-09-07 12:54:13 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.default_worker_options
|
2014-03-19 20:11:12 -04:00
|
|
|
|
defined?(@default_worker_options) ? @default_worker_options : { 'retry' => true, 'queue' => 'default' }
|
2013-09-07 12:54:13 -04:00
|
|
|
|
end
|
|
|
|
|
|
2012-04-22 22:22:09 -04:00
|
|
|
|
def self.load_json(string)
|
2013-05-12 17:33:49 -04:00
|
|
|
|
JSON.parse(string)
|
2012-04-22 17:02:35 -04:00
|
|
|
|
end
|
|
|
|
|
|
2012-04-22 22:22:09 -04:00
|
|
|
|
def self.dump_json(object)
|
2013-05-12 17:33:49 -04:00
|
|
|
|
JSON.generate(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
|
|
|
|
|
|
2014-06-07 18:12:31 -04:00
|
|
|
|
# See sidekiq/scheduled.rb for an in-depth explanation of this value
|
2012-06-19 19:36:59 -04:00
|
|
|
|
def self.poll_interval=(interval)
|
|
|
|
|
self.options[:poll_interval] = interval
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-23 18:44:37 -04:00
|
|
|
|
# Register a proc to handle any error which occurs within the Sidekiq process.
|
|
|
|
|
#
|
|
|
|
|
# Sidekiq.configure_server do |config|
|
|
|
|
|
# config.error_handlers << Proc.new {|ex,ctx_hash| MyErrorService.notify(ex, ctx_hash) }
|
|
|
|
|
# end
|
|
|
|
|
#
|
|
|
|
|
# The default error handler logs errors to Sidekiq.logger.
|
2014-02-24 23:47:44 -05:00
|
|
|
|
def self.error_handlers
|
|
|
|
|
self.options[:error_handlers]
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-23 18:44:37 -04:00
|
|
|
|
# Register a block to run at a point in the Sidekiq lifecycle.
|
|
|
|
|
# :startup, :quiet or :shutdown are valid events.
|
|
|
|
|
#
|
|
|
|
|
# Sidekiq.configure_server do |config|
|
|
|
|
|
# config.on(:shutdown) do
|
|
|
|
|
# puts "Goodbye cruel world!"
|
|
|
|
|
# end
|
|
|
|
|
# end
|
2014-03-10 23:46:19 -04:00
|
|
|
|
def self.on(event, &block)
|
2014-07-10 09:49:21 -04:00
|
|
|
|
raise ArgumentError, "Symbols only please: #{event}" unless event.is_a?(Symbol)
|
2014-12-17 15:08:43 -05:00
|
|
|
|
raise ArgumentError, "Invalid event name: #{event}" unless options[:lifecycle_events].key?(event)
|
2014-03-10 23:46:19 -04:00
|
|
|
|
options[:lifecycle_events][event] << block
|
|
|
|
|
end
|
2012-02-17 16:39:36 -05:00
|
|
|
|
end
|
2013-04-18 12:11:49 -04:00
|
|
|
|
|
|
|
|
|
require 'sidekiq/extensions/class_methods'
|
|
|
|
|
require 'sidekiq/extensions/action_mailer'
|
|
|
|
|
require 'sidekiq/extensions/active_record'
|
|
|
|
|
require 'sidekiq/rails' if defined?(::Rails::Engine)
|