2012-02-29 23:50:18 -05:00
|
|
|
require 'time'
|
2012-02-14 12:00:26 -05:00
|
|
|
require 'logger'
|
|
|
|
|
2012-01-22 14:32:38 -05:00
|
|
|
module Sidekiq
|
2012-02-17 16:39:36 -05:00
|
|
|
##
|
|
|
|
# This module is part of Sidekiq core and not intended for extensions.
|
|
|
|
#
|
2012-01-22 14:32:38 -05:00
|
|
|
module Util
|
|
|
|
|
2012-02-25 16:43:53 -05:00
|
|
|
class Pretty < Logger::Formatter
|
|
|
|
# Provide a call() method that returns the formatted message.
|
|
|
|
def call(severity, time, program_name, message)
|
|
|
|
"#{time.utc.iso8601} #{Process.pid} TID-#{Thread.current.object_id.to_s(36)} #{severity}: #{message}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-14 12:00:26 -05:00
|
|
|
def self.logger
|
|
|
|
@logger ||= begin
|
2012-02-25 16:43:53 -05:00
|
|
|
log = Logger.new(STDOUT)
|
2012-02-14 12:00:26 -05:00
|
|
|
log.level = Logger::INFO
|
2012-02-25 16:43:53 -05:00
|
|
|
log.formatter = Pretty.new
|
2012-02-14 12:00:26 -05:00
|
|
|
log
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.logger=(log)
|
|
|
|
@logger = (log ? log : Logger.new('/dev/null'))
|
|
|
|
end
|
|
|
|
|
2012-01-26 15:45:04 -05:00
|
|
|
def constantize(camel_cased_word)
|
|
|
|
names = camel_cased_word.split('::')
|
|
|
|
names.shift if names.empty? || names.first.empty?
|
|
|
|
|
|
|
|
constant = Object
|
|
|
|
names.each do |name|
|
|
|
|
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
|
|
|
|
end
|
|
|
|
constant
|
|
|
|
end
|
|
|
|
|
2012-01-22 14:32:38 -05:00
|
|
|
def watchdog(last_words)
|
|
|
|
yield
|
|
|
|
rescue => ex
|
2012-02-14 12:00:26 -05:00
|
|
|
logger.error last_words
|
|
|
|
logger.error ex
|
|
|
|
logger.error ex.backtrace.join("\n")
|
2012-01-22 14:32:38 -05:00
|
|
|
end
|
|
|
|
|
2012-02-14 12:00:26 -05:00
|
|
|
def logger
|
|
|
|
Sidekiq::Util.logger
|
2012-01-22 19:01:46 -05:00
|
|
|
end
|
2012-02-11 02:16:12 -05:00
|
|
|
|
2012-03-14 12:56:13 -04:00
|
|
|
def redis(&block)
|
|
|
|
Sidekiq.redis(&block)
|
2012-02-11 02:16:12 -05:00
|
|
|
end
|
2012-02-15 15:30:31 -05:00
|
|
|
|
|
|
|
def process_id
|
2012-03-06 23:18:16 -05:00
|
|
|
Process.pid
|
2012-02-15 15:30:31 -05:00
|
|
|
end
|
2012-01-22 14:32:38 -05:00
|
|
|
end
|
|
|
|
end
|