2022-05-31 16:37:31 -04:00
|
|
|
module Sidekiq
|
|
|
|
##
|
|
|
|
# Sidekiq::Component assumes a config instance is available at @config
|
2022-06-08 15:26:06 -04:00
|
|
|
module Component # :nodoc:
|
2022-05-31 16:37:31 -04:00
|
|
|
attr_reader :config
|
|
|
|
|
|
|
|
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.name = name
|
|
|
|
watchdog(name, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def logger
|
|
|
|
config.logger
|
|
|
|
end
|
|
|
|
|
|
|
|
def redis(&block)
|
|
|
|
config.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.pid}:#{process_nonce}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_exception(ex, ctx = {})
|
|
|
|
config.handle_exception(ex, ctx)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fire_event(event, options = {})
|
2022-07-28 15:21:42 -04:00
|
|
|
oneshot = options.fetch(:oneshot, true)
|
2022-05-31 16:37:31 -04:00
|
|
|
reverse = options[:reverse]
|
|
|
|
reraise = options[:reraise]
|
2022-08-30 14:51:23 -04:00
|
|
|
logger.debug("Firing #{event} event") if oneshot
|
2022-05-31 16:37:31 -04:00
|
|
|
|
|
|
|
arr = config[:lifecycle_events][event]
|
|
|
|
arr.reverse! if reverse
|
|
|
|
arr.each do |block|
|
|
|
|
block.call
|
|
|
|
rescue => ex
|
|
|
|
handle_exception(ex, {context: "Exception during Sidekiq lifecycle event.", event: event})
|
|
|
|
raise ex if reraise
|
|
|
|
end
|
2022-07-28 15:21:42 -04:00
|
|
|
arr.clear if oneshot # once we've fired an event, we never fire it again
|
2022-05-31 16:37:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|