2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2012-09-16 10:28:25 -04:00
|
|
|
require 'socket'
|
2014-12-30 20:25:55 -05:00
|
|
|
require 'securerandom'
|
2012-08-02 23:46:06 -04:00
|
|
|
require 'sidekiq/exception_handler'
|
2012-10-27 15:48:34 -04:00
|
|
|
require 'sidekiq/core_ext'
|
2012-08-02 23:46:06 -04:00
|
|
|
|
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
|
2012-08-02 23:46:06 -04:00
|
|
|
include ExceptionHandler
|
2012-01-22 14:32:38 -05:00
|
|
|
|
2012-09-08 22:50:03 -04:00
|
|
|
EXPIRY = 60 * 60 * 24
|
2012-04-24 10:15:29 -04:00
|
|
|
|
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 })
|
2013-09-22 17:05:29 -04:00
|
|
|
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
|
|
|
|
watchdog(name, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-14 12:00:26 -05:00
|
|
|
def logger
|
2012-05-15 22:44:35 -04:00
|
|
|
Sidekiq.logger
|
2012-01-22 19:01:46 -05:00
|
|
|
end
|
2012-02-11 02:16:12 -05:00
|
|
|
|
2012-03-14 12:56:13 -04:00
|
|
|
def redis(&block)
|
|
|
|
Sidekiq.redis(&block)
|
2012-02-11 02:16:12 -05:00
|
|
|
end
|
2012-02-15 15:30:31 -05:00
|
|
|
|
2012-09-16 10:27:49 -04:00
|
|
|
def hostname
|
2014-05-30 00:26:11 -04:00
|
|
|
ENV['DYNO'] || Socket.gethostname
|
2012-09-16 10:27:49 -04:00
|
|
|
end
|
2014-03-02 19:36:00 -05:00
|
|
|
|
2014-12-30 20:25:55 -05:00
|
|
|
def process_nonce
|
|
|
|
@@process_nonce ||= SecureRandom.hex(6)
|
|
|
|
end
|
|
|
|
|
2014-03-02 19:36:00 -05:00
|
|
|
def identity
|
2014-12-30 20:25:55 -05:00
|
|
|
@@identity ||= "#{hostname}:#{$$}:#{process_nonce}"
|
2014-03-02 19:36:00 -05:00
|
|
|
end
|
2014-05-14 00:41:40 -04:00
|
|
|
|
2015-06-02 23:33:01 -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
|
2014-12-17 15:09:20 -05:00
|
|
|
handle_exception(ex, { 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
|