Module: Sidekiq::Component

Overview

Sidekiq::Component assumes a config instance is available at @config

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

:nodoc:



7
8
9
# File 'lib/sidekiq/component.rb', line 7

def config
  @config
end

Instance Method Details

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sidekiq/component.rb', line 51

def fire_event(event, options = {})
  oneshot = options.fetch(:oneshot, true)
  reverse = options[:reverse]
  reraise = options[:reraise]
  logger.debug("Firing #{event} event") if oneshot

  arr = config[: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 if oneshot # once we've fired an event, we never fire it again
end

#handle_exception(ex, ctx = {}) ⇒ Object



47
48
49
# File 'lib/sidekiq/component.rb', line 47

def handle_exception(ex, ctx = {})
  config.handle_exception(ex, ctx)
end

#hostnameObject



35
36
37
# File 'lib/sidekiq/component.rb', line 35

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

#identityObject



43
44
45
# File 'lib/sidekiq/component.rb', line 43

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

#loggerObject



23
24
25
# File 'lib/sidekiq/component.rb', line 23

def logger
  config.logger
end

#process_nonceObject



39
40
41
# File 'lib/sidekiq/component.rb', line 39

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

#redis(&block) ⇒ Object



27
28
29
# File 'lib/sidekiq/component.rb', line 27

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

#safe_thread(name, &block) ⇒ Object



16
17
18
19
20
21
# File 'lib/sidekiq/component.rb', line 16

def safe_thread(name, &block)
  Thread.new do
    Thread.current.name = "sidekiq.#{name}"
    watchdog(name, &block)
  end
end

#tidObject



31
32
33
# File 'lib/sidekiq/component.rb', line 31

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

#watchdog(last_words) ⇒ Object



9
10
11
12
13
14
# File 'lib/sidekiq/component.rb', line 9

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