1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/util.rb
Mike Perham 022c63d86f Rework logger changes a bit
* logger_formatter -> log_format
* avoid touching global Sidekiq.logger in JobLogger
* rewrite tests for compactness and coverage
* hide banner in JSON
2018-12-29 07:54:05 -08:00

70 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require 'socket'
require 'securerandom'
require 'sidekiq/exception_handler'
module Sidekiq
##
# This module is part of Sidekiq core and not intended for extensions.
#
module Util
include ExceptionHandler
EXPIRY = 60 * 60 * 24
def watchdog(last_words)
yield
rescue Exception => ex
handle_exception(ex, { context: last_words })
raise ex
end
def safe_thread(name, &block)
Thread.new do
Thread.current['sidekiq_label'] = name
watchdog(name, &block)
end
end
def logger
Sidekiq.logger
end
def redis(&block)
Sidekiq.redis(&block)
end
def tid
Thread.current['sidekiq_tid'] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
end
def hostname
ENV['DYNO'] || Socket.gethostname
end
def process_nonce
@@process_nonce ||= SecureRandom.hex(6)
end
def identity
@@identity ||= "#{hostname}:#{$$}:#{process_nonce}"
end
def fire_event(event, options={})
reverse = options[:reverse]
reraise = options[:reraise]
arr = Sidekiq.options[:lifecycle_events][event]
arr.reverse! if reverse
arr.each do |block|
begin
block.call
rescue => ex
handle_exception(ex, { context: "Exception during Sidekiq lifecycle event.", event: event })
raise ex if reraise
end
end
arr.clear
end
end
end