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

70 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "socket"
require "securerandom"
require "sidekiq/exception_handler"
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
include ExceptionHandler
2012-01-22 14:32:38 -05:00
2012-09-08 22:50:03 -04:00
EXPIRY = 60 * 60 * 24
2012-01-22 14:32:38 -05:00
def watchdog(last_words)
yield
2012-08-29 23:20:20 -04:00
rescue Exception => ex
handle_exception(ex, {context: last_words})
raise ex
2012-01-22 14:32:38 -05:00
end
2015-10-02 18:44:29 -04:00
def safe_thread(name, &block)
Thread.new do
Thread.current["sidekiq_label"] = name
2015-10-02 18:44:29 -04:00
watchdog(name, &block)
end
end
def logger
Sidekiq.logger
2012-01-22 19:01:46 -05:00
end
def redis(&block)
Sidekiq.redis(&block)
end
def tid
Thread.current["sidekiq_tid"] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
end
2012-09-16 10:27:49 -04:00
def hostname
ENV["DYNO"] || Socket.gethostname
2012-09-16 10:27:49 -04:00
end
def process_nonce
2019-04-26 15:36:33 -04:00
@@process_nonce ||= SecureRandom.hex(6)
end
def identity
2019-04-26 15:36:33 -04:00
@@identity ||= "#{hostname}:#{::Process.pid}:#{process_nonce}"
end
2014-05-14 00:41:40 -04:00
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|
block.call
rescue => ex
handle_exception(ex, {context: "Exception during Sidekiq lifecycle event.", event: event})
raise ex if reraise
2014-05-14 00:41:40 -04:00
end
2015-10-13 12:46:06 -04:00
arr.clear
2014-05-14 00:41:40 -04:00
end
2012-01-22 14:32:38 -05:00
end
end