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:



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

def config
  @config
end

Instance Method Details

#default_tag(dir = Dir.pwd) ⇒ Object



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

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



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

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



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

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

#hostnameObject



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

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

#identityObject



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

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



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

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



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

def logger
  config.logger
end

#mono_msObject

used for time difference and relative comparisons, not persistence.



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

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

#process_nonceObject



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

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

#real_msObject

This is epoch milliseconds, appropriate for persistence



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

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

#redis(&block) ⇒ Object



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

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

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



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

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



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

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

#watchdog(last_words) ⇒ Object



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

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