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

67 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2012-09-16 10:28:25 -04:00
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
def self.tid
Thread.current['sidekiq_tid'] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
end
2012-01-22 14:32:38 -05:00
def watchdog(last_words)
yield
2012-08-29 23:20:20 -04:00
rescue Exception => ex
2014-12-17 15:09:20 -05:00
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'.freeze] = 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
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
@@process_nonce ||= SecureRandom.hex(6)
end
def identity
@@identity ||= "#{hostname}:#{$$}:#{process_nonce}"
end
2014-05-14 00:41:40 -04:00
def fire_event(event, reverse=false)
arr = Sidekiq.options[:lifecycle_events][event]
arr.reverse! if reverse
arr.each do |block|
2014-05-14 00:41:40 -04:00
begin
block.call
rescue => ex
handle_exception(ex, { context: "Exception during Sidekiq lifecycle event.", event: event })
2014-05-14 00:41:40 -04:00
end
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