lib/sidekiq.rb in sidekiq-5.2.10 vs lib/sidekiq.rb in sidekiq-6.0.0.pre1

- old
+ new

@@ -1,29 +1,29 @@ # frozen_string_literal: true -require 'sidekiq/version' -fail "Sidekiq #{Sidekiq::VERSION} does not support Ruby versions below 2.2.2." if RUBY_PLATFORM != 'java' && Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.2.2') +require "sidekiq/version" +fail "Sidekiq #{Sidekiq::VERSION} does not support Ruby versions below 2.5.0." if RUBY_PLATFORM != "java" && Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.5.0") -require 'sidekiq/logging' -require 'sidekiq/client' -require 'sidekiq/worker' -require 'sidekiq/redis_connection' -require 'sidekiq/delay' +require "sidekiq/logger" +require "sidekiq/client" +require "sidekiq/worker" +require "sidekiq/redis_connection" +require "sidekiq/delay" -require 'json' +require "json" module Sidekiq - NAME = 'Sidekiq' - LICENSE = 'See LICENSE and the LGPL-3.0 for licensing details.' + NAME = "Sidekiq" + LICENSE = "See LICENSE and the LGPL-3.0 for licensing details." DEFAULTS = { queues: [], labels: [], concurrency: 10, - require: '.', + require: ".", environment: nil, - timeout: 8, + timeout: 25, poll_interval_average: nil, average_scheduled_poll_interval: 5, error_handlers: [], death_handlers: [], lifecycle_events: { @@ -36,20 +36,20 @@ dead_timeout_in_seconds: 180 * 24 * 60 * 60, # 6 months reloader: proc { |&block| block.call }, } DEFAULT_WORKER_OPTIONS = { - 'retry' => true, - 'queue' => 'default' + "retry" => true, + "queue" => "default", } FAKE_INFO = { "redis_version" => "9.9.9", "uptime_in_days" => "9999", "connected_clients" => "9999", "used_memory_human" => "9P", - "used_memory_peak_human" => "9P" + "used_memory_peak_human" => "9P", } def self.❨╯°□°❩╯︵┻━┻ puts "Calm down, yo." end @@ -94,33 +94,35 @@ redis_pool.with do |conn| retryable = true begin yield conn rescue Redis::CommandError => ex - #2550 Failover can cause the server to become a replica, need + # 2550 Failover can cause the server to become a replica, need # to disconnect and reopen the socket to get back to the primary. - (conn.disconnect!; retryable = false; retry) if retryable && ex.message =~ /READONLY/ + if retryable && ex.message =~ /READONLY/ + conn.disconnect! + retryable = false + retry + end raise end end end def self.redis_info redis do |conn| - begin - # admin commands can't go through redis-namespace starting - # in redis-namespace 2.0 - if conn.respond_to?(:namespace) - conn.redis.info - else - conn.info - end - rescue Redis::CommandError => ex - #2850 return fake version when INFO command has (probably) been renamed - raise unless ex.message =~ /unknown command/ - FAKE_INFO + # admin commands can't go through redis-namespace starting + # in redis-namespace 2.0 + if conn.respond_to?(:namespace) + conn.redis.info + else + conn.info end + rescue Redis::CommandError => ex + # 2850 return fake version when INFO command has (probably) been renamed + raise unless ex.message =~ /unknown command/ + FAKE_INFO end end def self.redis_pool @redis ||= Sidekiq::RedisConnection.create @@ -150,22 +152,17 @@ Middleware::Chain.new end def self.default_worker_options=(hash) # stringify - @default_worker_options = default_worker_options.merge(Hash[hash.map{|k, v| [k.to_s, v]}]) + @default_worker_options = default_worker_options.merge(Hash[hash.map {|k, v| [k.to_s, v]}]) end + def self.default_worker_options defined?(@default_worker_options) ? @default_worker_options : DEFAULT_WORKER_OPTIONS end - def self.default_retries_exhausted=(prok) - logger.info { "default_retries_exhausted is deprecated, please use `config.death_handlers << -> {|job, ex| }`" } - return nil unless prok - death_handlers << prok - end - ## # Death handlers are called when all retries for a job have been exhausted and # the job dies. It's the notification to your application # that this job will not succeed without manual intervention. # @@ -178,39 +175,53 @@ end def self.load_json(string) JSON.parse(string) end + def self.dump_json(object) JSON.generate(object) end + def self.log_formatter + @log_formatter ||= if ENV["DYNO"] + Sidekiq::Logger::Formatters::WithoutTimestamp.new + else + Sidekiq::Logger::Formatters::Pretty.new + end + end + + def self.log_formatter=(log_formatter) + @log_formatter = log_formatter + end + def self.logger - Sidekiq::Logging.logger + @logger ||= Sidekiq::Logger.new(STDOUT, level: Logger::INFO) end - def self.logger=(log) - Sidekiq::Logging.logger = log + + def self.logger=(logger) + @logger = logger end # How frequently Redis should be checked by a random Sidekiq process for # scheduled and retriable jobs. Each individual process will take turns by # waiting some multiple of this value. # # See sidekiq/scheduled.rb for an in-depth explanation of this value def self.average_scheduled_poll_interval=(interval) - self.options[:average_scheduled_poll_interval] = interval + options[:average_scheduled_poll_interval] = interval end # Register a proc to handle any error which occurs within the Sidekiq process. # # Sidekiq.configure_server do |config| # config.error_handlers << proc {|ex,ctx_hash| MyErrorService.notify(ex, ctx_hash) } # end # # The default error handler logs errors to Sidekiq.logger. def self.error_handlers - self.options[:error_handlers] + options[:error_handlers] end # Register a block to run at a point in the Sidekiq lifecycle. # :startup, :quiet or :shutdown are valid events. # @@ -232,6 +243,6 @@ # otherwise Ruby's Thread#kill will commit. See #377. # DO NOT RESCUE THIS ERROR IN YOUR WORKERS class Shutdown < Interrupt; end end -require 'sidekiq/rails' if defined?(::Rails::Engine) +require "sidekiq/rails" if defined?(::Rails::Engine)