mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Update Sidekiq logging to use standard Ruby logger
This commit is contained in:
parent
4c7ae756c8
commit
b210dd788c
7 changed files with 60 additions and 45 deletions
|
@ -19,6 +19,7 @@ module Sidekiq
|
||||||
class CLI
|
class CLI
|
||||||
include Util
|
include Util
|
||||||
|
|
||||||
|
# Used for CLI testing
|
||||||
attr_accessor :options, :code
|
attr_accessor :options, :code
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
|
@ -26,6 +27,7 @@ module Sidekiq
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(args=ARGV)
|
def parse(args=ARGV)
|
||||||
|
Sidekiq::Util.logger
|
||||||
parse_options(args)
|
parse_options(args)
|
||||||
validate!
|
validate!
|
||||||
boot_system
|
boot_system
|
||||||
|
@ -35,14 +37,14 @@ module Sidekiq
|
||||||
Sidekiq::Manager.redis = RedisConnection.create(:url => @options[:server], :namespace => @options[:namespace])
|
Sidekiq::Manager.redis = RedisConnection.create(:url => @options[:server], :namespace => @options[:namespace])
|
||||||
manager = Sidekiq::Manager.new(@options)
|
manager = Sidekiq::Manager.new(@options)
|
||||||
begin
|
begin
|
||||||
log 'Starting processing, hit Ctrl-C to stop'
|
logger.info 'Starting processing, hit Ctrl-C to stop'
|
||||||
manager.start!
|
manager.start!
|
||||||
# HACK need to determine how to pause main thread while
|
# HACK need to determine how to pause main thread while
|
||||||
# waiting for signals.
|
# waiting for signals.
|
||||||
sleep
|
sleep
|
||||||
rescue Interrupt
|
rescue Interrupt
|
||||||
# TODO Need clean shutdown support from Celluloid
|
# TODO Need clean shutdown support from Celluloid
|
||||||
log 'Shutting down, pausing 5 seconds to let workers finish...'
|
logger.info 'Shutting down, pausing 5 seconds to let workers finish...'
|
||||||
manager.stop!
|
manager.stop!
|
||||||
manager.wait(:shutdown)
|
manager.wait(:shutdown)
|
||||||
end
|
end
|
||||||
|
@ -75,22 +77,19 @@ module Sidekiq
|
||||||
@options[:queues] << 'default' if @options[:queues].empty?
|
@options[:queues] << 'default' if @options[:queues].empty?
|
||||||
@options[:queues].shuffle!
|
@options[:queues].shuffle!
|
||||||
|
|
||||||
$DEBUG = @options[:verbose]
|
|
||||||
|
|
||||||
if !File.exist?(@options[:require]) ||
|
if !File.exist?(@options[:require]) ||
|
||||||
(File.directory?(@options[:require]) && !File.exist?("#{@options[:require]}/config/application.rb"))
|
(File.directory?(@options[:require]) && !File.exist?("#{@options[:require]}/config/application.rb"))
|
||||||
log "=================================================================="
|
logger.info "=================================================================="
|
||||||
log " Please point sidekiq to a Rails 3 application or a Ruby file "
|
logger.info " Please point sidekiq to a Rails 3 application or a Ruby file "
|
||||||
log " to load your worker classes with -r [DIR|FILE]."
|
logger.info " to load your worker classes with -r [DIR|FILE]."
|
||||||
log "=================================================================="
|
logger.info "=================================================================="
|
||||||
log @parser
|
logger.info @parser
|
||||||
die(1)
|
die(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_options(argv)
|
def parse_options(argv)
|
||||||
@options = {
|
@options = {
|
||||||
:verbose => false,
|
|
||||||
:queues => [],
|
:queues => [],
|
||||||
:processor_count => 25,
|
:processor_count => 25,
|
||||||
:require => '.',
|
:require => '.',
|
||||||
|
@ -106,7 +105,7 @@ module Sidekiq
|
||||||
end
|
end
|
||||||
|
|
||||||
o.on "-v", "--verbose", "Print more verbose output" do
|
o.on "-v", "--verbose", "Print more verbose output" do
|
||||||
@options[:verbose] = true
|
Sidekiq::Util.logger.level = Logger::DEBUG
|
||||||
end
|
end
|
||||||
|
|
||||||
o.on "-n", "--namespace NAMESPACE", "namespace worker queues are under" do |arg|
|
o.on "-n", "--namespace NAMESPACE", "namespace worker queues are under" do |arg|
|
||||||
|
@ -132,7 +131,7 @@ module Sidekiq
|
||||||
|
|
||||||
@parser.banner = "sidekiq [options]"
|
@parser.banner = "sidekiq [options]"
|
||||||
@parser.on_tail "-h", "--help", "Show help" do
|
@parser.on_tail "-h", "--help", "Show help" do
|
||||||
log @parser
|
logger.info @parser
|
||||||
die 1
|
die 1
|
||||||
end
|
end
|
||||||
@parser.parse!(argv)
|
@parser.parse!(argv)
|
||||||
|
|
|
@ -23,8 +23,8 @@ module Sidekiq
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(options={})
|
def initialize(options={})
|
||||||
log "Booting sidekiq #{Sidekiq::VERSION} with Redis at #{redis.client.location}"
|
logger.info "Booting sidekiq #{Sidekiq::VERSION} with Redis at #{redis.client.location}"
|
||||||
verbose options.inspect
|
logger.debug { options.inspect }
|
||||||
@count = options[:processor_count] || 25
|
@count = options[:processor_count] || 25
|
||||||
@queues = options[:queues]
|
@queues = options[:queues]
|
||||||
@done_callback = nil
|
@done_callback = nil
|
||||||
|
@ -74,11 +74,6 @@ module Sidekiq
|
||||||
def processor_died(processor, reason)
|
def processor_died(processor, reason)
|
||||||
@busy.delete(processor)
|
@busy.delete(processor)
|
||||||
|
|
||||||
if reason
|
|
||||||
err "Processor death: #{reason}"
|
|
||||||
err reason.backtrace.join("\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
unless stopped?
|
unless stopped?
|
||||||
@ready << Processor.new_link(current_actor)
|
@ready << Processor.new_link(current_actor)
|
||||||
dispatch
|
dispatch
|
||||||
|
@ -103,18 +98,20 @@ module Sidekiq
|
||||||
|
|
||||||
# Dispatch loop
|
# Dispatch loop
|
||||||
loop do
|
loop do
|
||||||
break verbose('no processors') if @ready.empty?
|
break logger.debug('no processors') if @ready.empty?
|
||||||
found = false
|
found = false
|
||||||
@ready.size.times do
|
@ready.size.times do
|
||||||
found ||= find_work(@queues.sample)
|
found ||= find_work(@queues.sample)
|
||||||
end
|
end
|
||||||
break verbose('nothing to process') unless found
|
break logger.debug('nothing to process') unless found
|
||||||
end
|
end
|
||||||
|
|
||||||
# This is the polling loop that ensures we check Redis every
|
# This is the polling loop that ensures we check Redis every
|
||||||
# second for work, even if there was nothing to do this time
|
# second for work, even if there was nothing to do this time
|
||||||
# around.
|
# around.
|
||||||
after(1) { verbose('ping'); dispatch(schedule) } if schedule
|
after(1) do
|
||||||
|
dispatch(schedule)
|
||||||
|
end if schedule
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
|
require 'sidekiq/util'
|
||||||
|
|
||||||
module Sidekiq
|
module Sidekiq
|
||||||
module Middleware
|
module Middleware
|
||||||
module Server
|
module Server
|
||||||
class Airbrake
|
class Airbrake
|
||||||
|
include Util
|
||||||
def call(*args)
|
def call(*args)
|
||||||
yield
|
yield
|
||||||
rescue => ex
|
rescue => ex
|
||||||
|
logger.warn ex
|
||||||
|
logger.warn ex.backtrace.join("\n")
|
||||||
send_to_airbrake(args[1], ex) if defined?(::Airbrake)
|
send_to_airbrake(args[1], ex) if defined?(::Airbrake)
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,17 +2,27 @@ module Sidekiq
|
||||||
module Worker
|
module Worker
|
||||||
|
|
||||||
##
|
##
|
||||||
# The Sidekiq testing infrastructure just overrides perform_async
|
# The Sidekiq testing infrastructure overrides perform_async
|
||||||
# so that it does not actually touch the network. Instead it
|
# so that it does not actually touch the network. Instead it
|
||||||
# just stores the asynchronous jobs in a per-class array so that
|
# stores the asynchronous jobs in a per-class array so that
|
||||||
# their presence/absence can be asserted by your tests.
|
# their presence/absence can be asserted by your tests.
|
||||||
#
|
#
|
||||||
# This is similar to ActionMailer's :test delivery_method and its
|
# This is similar to ActionMailer's :test delivery_method and its
|
||||||
# ActionMailer::Base.deliveries array.
|
# ActionMailer::Base.deliveries array.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
#
|
||||||
|
# require 'sidekiq/testing'
|
||||||
|
#
|
||||||
|
# assert_equal 0, HardWorker.jobs.size
|
||||||
|
# HardWorker.perform_async(:something)
|
||||||
|
# assert_equal 1, HardWorker.jobs.size
|
||||||
|
# assert_equal :something, HardWorker.jobs[0]['args'][0]
|
||||||
|
#
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
alias_method :perform_async_old, :perform_async
|
alias_method :perform_async_old, :perform_async
|
||||||
def perform_async(*args)
|
def perform_async(*args)
|
||||||
jobs << args
|
jobs << { 'class' => self.name, 'args' => args }
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,20 @@
|
||||||
|
require 'logger'
|
||||||
|
|
||||||
module Sidekiq
|
module Sidekiq
|
||||||
module Util
|
module Util
|
||||||
|
|
||||||
|
def self.logger
|
||||||
|
@logger ||= begin
|
||||||
|
log = Logger.new(STDERR)
|
||||||
|
log.level = Logger::INFO
|
||||||
|
log
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.logger=(log)
|
||||||
|
@logger = (log ? log : Logger.new('/dev/null'))
|
||||||
|
end
|
||||||
|
|
||||||
def constantize(camel_cased_word)
|
def constantize(camel_cased_word)
|
||||||
names = camel_cased_word.split('::')
|
names = camel_cased_word.split('::')
|
||||||
names.shift if names.empty? || names.first.empty?
|
names.shift if names.empty? || names.first.empty?
|
||||||
|
@ -15,21 +29,13 @@ module Sidekiq
|
||||||
def watchdog(last_words)
|
def watchdog(last_words)
|
||||||
yield
|
yield
|
||||||
rescue => ex
|
rescue => ex
|
||||||
err last_words
|
logger.error last_words
|
||||||
err ex
|
logger.error ex
|
||||||
err ex.backtrace.join("\n")
|
logger.error ex.backtrace.join("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
def err(msg)
|
def logger
|
||||||
STDERR.puts(msg)
|
Sidekiq::Util.logger
|
||||||
end
|
|
||||||
|
|
||||||
def log(msg)
|
|
||||||
STDOUT.puts(msg) unless $TESTING
|
|
||||||
end
|
|
||||||
|
|
||||||
def verbose(msg)
|
|
||||||
STDOUT.puts(msg) if $DEBUG
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def redis
|
def redis
|
||||||
|
|
|
@ -24,13 +24,8 @@ module Sidekiq
|
||||||
base.extend(ClassMethods)
|
base.extend(ClassMethods)
|
||||||
end
|
end
|
||||||
|
|
||||||
def info(msg)
|
def logger
|
||||||
print "#{msg}\n"
|
Sidekiq::Util.logger
|
||||||
end
|
|
||||||
alias_method :log, :info
|
|
||||||
|
|
||||||
def debug(msg)
|
|
||||||
print "#{msg}\n" if $DEBUG
|
|
||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
|
|
@ -7,3 +7,6 @@ end
|
||||||
require 'minitest/unit'
|
require 'minitest/unit'
|
||||||
require 'minitest/pride'
|
require 'minitest/pride'
|
||||||
require 'minitest/autorun'
|
require 'minitest/autorun'
|
||||||
|
|
||||||
|
require 'sidekiq/util'
|
||||||
|
Sidekiq::Util.logger.level = Logger::ERROR
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue