Module: Sidekiq::Util

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

Constant Summary collapse

PAUSE_TIME =

hack for quicker development / testing environment #2774

$stdout.tty? ? 0.1 : 0.5

Instance Method Summary collapse

Methods included from ExceptionHandler

#handle_exception

Instance Method Details

#fire_event(event, options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sidekiq/util.rb', line 93

def fire_event(event, options = {})
  reverse = options[:reverse]
  reraise = options[:reraise]

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

#hostnameObject



81
82
83
# File 'lib/sidekiq/util.rb', line 81

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

#identityObject



89
90
91
# File 'lib/sidekiq/util.rb', line 89

def identity
  @@identity ||= "#{hostname}:#{::Process.pid}:#{process_nonce}"
end

#loggerObject



69
70
71
# File 'lib/sidekiq/util.rb', line 69

def logger
  Sidekiq.logger
end

#process_nonceObject



85
86
87
# File 'lib/sidekiq/util.rb', line 85

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

#redis(&block) ⇒ Object



73
74
75
# File 'lib/sidekiq/util.rb', line 73

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

#safe_thread(name, &block) ⇒ Object



62
63
64
65
66
67
# File 'lib/sidekiq/util.rb', line 62

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

#tidObject



77
78
79
# File 'lib/sidekiq/util.rb', line 77

def tid
  Thread.current["sidekiq_tid"] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
end

#wait_for(deadline, &condblock) ⇒ Object

Wait for the orblock to be true or the deadline passed.



46
47
48
49
50
51
52
53
# File 'lib/sidekiq/util.rb', line 46

def wait_for(deadline, &condblock)
  remaining = deadline - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  while remaining > PAUSE_TIME
    return if condblock.call
    sleep PAUSE_TIME
    remaining = deadline - ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  end
end

#watchdog(last_words) ⇒ Object



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

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