Module: Sidekiq::Component

Overview

Sidekiq::Component provides a set of utility methods depending only on Sidekiq::Config. It assumes a config instance is available at @config.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

:nodoc:



25
26
27
# File 'lib/sidekiq/component.rb', line 25

def config
  @config
end

Instance Method Details

#default_tag(dir = Dir.pwd) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/sidekiq/component.rb', line 118

def default_tag(dir = Dir.pwd)
  name = File.basename(dir)
  prevdir = File.dirname(dir) # Capistrano release directory?
  if name.to_i != 0 && prevdir
    if File.basename(prevdir) == "releases"
      return File.basename(File.dirname(prevdir))
    end
  end
  name
end

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sidekiq/component.rb', line 79

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



75
76
77
# File 'lib/sidekiq/component.rb', line 75

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

#hostnameObject



63
64
65
# File 'lib/sidekiq/component.rb', line 63

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

#identityObject



71
72
73
# File 'lib/sidekiq/component.rb', line 71

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

#inspectObject

When you have a large tree of components, the ‘inspect` output can get out of hand, especially with lots of Sidekiq::Config references everywhere. We avoid calling `inspect` on more complex state and use `to_s` instead to keep output manageable, #6553



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sidekiq/component.rb', line 100

def inspect
  "#<#{self.class.name} #{
    instance_variables.map do |name|
      value = instance_variable_get(name)
      case value
      when Proc
        "#{name}=#{value}"
      when Sidekiq::Config
        "#{name}=#{value}"
      when Sidekiq::Component
        "#{name}=#{value}"
      else
        "#{name}=#{value.inspect}"
      end
    end.join(", ")
  }>"
end

#loggerObject



51
52
53
# File 'lib/sidekiq/component.rb', line 51

def logger
  config.logger
end

#mono_msObject

used for time difference and relative comparisons, not persistence.



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

def mono_ms
  ::Process.clock_gettime(::Process::CLOCK_MONOTONIC, :millisecond)
end

#process_nonceObject



67
68
69
# File 'lib/sidekiq/component.rb', line 67

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

#real_msObject

This is epoch milliseconds, appropriate for persistence



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

def real_ms
  ::Process.clock_gettime(::Process::CLOCK_REALTIME, :millisecond)
end

#redis(&block) ⇒ Object



55
56
57
# File 'lib/sidekiq/component.rb', line 55

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

#safe_thread(name, priority: nil, &block) ⇒ Object



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

def safe_thread(name, priority: nil, &block)
  Thread.new do
    Thread.current.name = "sidekiq.#{name}"
    watchdog(name, &block)
  end.tap { |t| t.priority = (priority || config.thread_priority || DEFAULT_THREAD_PRIORITY) }
end

#tidObject



59
60
61
# File 'lib/sidekiq/component.rb', line 59

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

#watchdog(last_words) ⇒ Object



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

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