2012-09-16 07:28:25 -07:00
|
|
|
require 'socket'
|
2014-12-31 01:25:55 +00:00
|
|
|
require 'securerandom'
|
2012-08-02 20:46:06 -07:00
|
|
|
require 'sidekiq/exception_handler'
|
2012-10-27 12:48:34 -07:00
|
|
|
require 'sidekiq/core_ext'
|
2012-08-02 20:46:06 -07:00
|
|
|
|
2012-01-22 11:32:38 -08:00
|
|
|
module Sidekiq
|
2012-02-17 13:39:36 -08:00
|
|
|
##
|
|
|
|
# This module is part of Sidekiq core and not intended for extensions.
|
|
|
|
#
|
2012-01-22 11:32:38 -08:00
|
|
|
module Util
|
2012-08-02 20:46:06 -07:00
|
|
|
include ExceptionHandler
|
2012-01-22 11:32:38 -08:00
|
|
|
|
2012-09-08 19:50:03 -07:00
|
|
|
EXPIRY = 60 * 60 * 24
|
2012-04-24 07:15:29 -07:00
|
|
|
|
2012-01-22 11:32:38 -08:00
|
|
|
def watchdog(last_words)
|
|
|
|
yield
|
2012-08-29 20:20:20 -07:00
|
|
|
rescue Exception => ex
|
2014-12-17 21:09:20 +01:00
|
|
|
handle_exception(ex, { context: last_words })
|
2013-09-22 14:05:29 -07:00
|
|
|
raise ex
|
2012-01-22 11:32:38 -08:00
|
|
|
end
|
|
|
|
|
2012-02-14 09:00:26 -08:00
|
|
|
def logger
|
2012-05-15 19:44:35 -07:00
|
|
|
Sidekiq.logger
|
2012-01-22 16:01:46 -08:00
|
|
|
end
|
2012-02-10 23:16:12 -08:00
|
|
|
|
2012-03-14 09:56:13 -07:00
|
|
|
def redis(&block)
|
|
|
|
Sidekiq.redis(&block)
|
2012-02-10 23:16:12 -08:00
|
|
|
end
|
2012-02-15 12:30:31 -08:00
|
|
|
|
2012-09-16 07:27:49 -07:00
|
|
|
def hostname
|
2014-05-29 21:26:11 -07:00
|
|
|
ENV['DYNO'] || Socket.gethostname
|
2012-09-16 07:27:49 -07:00
|
|
|
end
|
2014-03-02 16:36:00 -08:00
|
|
|
|
2014-12-31 01:25:55 +00:00
|
|
|
def process_nonce
|
|
|
|
@@process_nonce ||= SecureRandom.hex(6)
|
|
|
|
end
|
|
|
|
|
2014-03-02 16:36:00 -08:00
|
|
|
def identity
|
2014-12-31 01:25:55 +00:00
|
|
|
@@identity ||= "#{hostname}:#{$$}:#{process_nonce}"
|
2014-03-02 16:36:00 -08:00
|
|
|
end
|
2014-05-13 21:41:40 -07:00
|
|
|
|
2015-06-02 20:33:01 -07:00
|
|
|
def fire_event(event, reverse=false)
|
|
|
|
arr = Sidekiq.options[:lifecycle_events][event]
|
|
|
|
arr.reverse! if reverse
|
|
|
|
arr.each do |block|
|
2014-05-13 21:41:40 -07:00
|
|
|
begin
|
|
|
|
block.call
|
|
|
|
rescue => ex
|
2014-12-17 21:09:20 +01:00
|
|
|
handle_exception(ex, { event: event })
|
2014-05-13 21:41:40 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-12 10:51:34 -07:00
|
|
|
def want_a_hertz_donut?
|
|
|
|
# what's a hertz donut?
|
|
|
|
# punch! Hurts, don't it?
|
|
|
|
info = Sidekiq.redis {|c| c.info }
|
|
|
|
if info['connected_clients'].to_i > 1000 && info['hz'].to_i >= 10
|
|
|
|
Sidekiq.logger.warn { "Your Redis `hz` setting is too high at #{info['hz']}. See mperham/sidekiq#2431. Set it to 3 in #{info[:config_file]}" }
|
|
|
|
true
|
|
|
|
else
|
|
|
|
Sidekiq.logger.debug { "Redis hz: #{info['hz']}. Client count: #{info['connected_clients']}" }
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-22 11:32:38 -08:00
|
|
|
end
|
|
|
|
end
|