Module: Sidekiq::Util

Included in:
CLI, Manager, Middleware::Server::ExceptionHandler, Processor
Defined in:
lib/sidekiq/util.rb

Overview

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

Defined Under Namespace

Classes: Pretty

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.loggerObject


17
18
19
20
21
22
23
24
# File 'lib/sidekiq/util.rb', line 17

def self.logger
  @logger ||= begin
    log = Logger.new(STDOUT)
    log.level = Logger::INFO
    log.formatter = Pretty.new
    log
  end
end

.logger=(log) ⇒ Object


26
27
28
# File 'lib/sidekiq/util.rb', line 26

def self.logger=(log)
  @logger = (log ? log : Logger.new('/dev/null'))
end

.process_idObject


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sidekiq/util.rb', line 61

def self.process_id
  @pid ||= begin
    if Process.pid == 1
      # Heroku does not expose pids.
      require 'securerandom'
      (SecureRandom.random_number * 4_000_000_000).floor.to_s(16)
    else
      Process.pid
    end
  end
end

Instance Method Details

#constantize(camel_cased_word) ⇒ Object


30
31
32
33
34
35
36
37
38
39
# File 'lib/sidekiq/util.rb', line 30

def constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#loggerObject


49
50
51
# File 'lib/sidekiq/util.rb', line 49

def logger
  Sidekiq::Util.logger
end

#process_idObject


57
58
59
# File 'lib/sidekiq/util.rb', line 57

def process_id
  Sidekiq::Util.process_id
end

#redisObject


53
54
55
# File 'lib/sidekiq/util.rb', line 53

def redis
  Sidekiq.redis
end

#watchdog(last_words) ⇒ Object


41
42
43
44
45
46
47
# File 'lib/sidekiq/util.rb', line 41

def watchdog(last_words)
  yield
rescue => ex
  logger.error last_words
  logger.error ex
  logger.error ex.backtrace.join("\n")
end