Module: Sidekiq::Util

Includes:
ExceptionHandler
Included in:
CLI, JobRetry, Launcher, Manager, Processor, Scheduled::Poller
Defined in:
lib/sidekiq/util.rb

Overview

This module is part of Sidekiq core and not intended for extensions.

Constant Summary collapse

EXPIRY =
60 * 60 * 24

Instance Method Summary collapse

Methods included from ExceptionHandler

#handle_exception

Instance Method Details

#fire_event(event, reverse = false) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sidekiq/util.rb', line 49

def fire_event(event, reverse=false)
  arr = Sidekiq.options[:lifecycle_events][event]
  arr.reverse! if reverse
  arr.each do |block|
    begin
      block.call
    rescue => ex
      handle_exception(ex, { context: "Exception during Sidekiq lifecycle event.", event: event })
    end
  end
  arr.clear
end

#hostnameObject


37
38
39
# File 'lib/sidekiq/util.rb', line 37

def hostname
  ENV['DYNO'] || Socket.gethostname
end

#identityObject


45
46
47
# File 'lib/sidekiq/util.rb', line 45

def identity
  @@identity ||= "#{hostname}:#{$$}:#{process_nonce}"
end

#loggerObject


29
30
31
# File 'lib/sidekiq/util.rb', line 29

def logger
  Sidekiq.logger
end

#process_nonceObject


41
42
43
# File 'lib/sidekiq/util.rb', line 41

def process_nonce
  @@process_nonce ||= SecureRandom.hex(6)
end

#redis(&block) ⇒ Object


33
34
35
# File 'lib/sidekiq/util.rb', line 33

def redis(&block)
  Sidekiq.redis(&block)
end

#safe_thread(name, &block) ⇒ Object


22
23
24
25
26
27
# File 'lib/sidekiq/util.rb', line 22

def safe_thread(name, &block)
  Thread.new do
    Thread.current['sidekiq_label'] = name
    watchdog(name, &block)
  end
end

#watchdog(last_words) ⇒ Object


15
16
17
18
19
20
# File 'lib/sidekiq/util.rb', line 15

def watchdog(last_words)
  yield
rescue Exception => ex
  handle_exception(ex, { context: last_words })
  raise ex
end