Class: SidekiqVigil::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_vigil/reporter.rb

Constant Summary collapse

STATS_TTL =
8 * 24 * 60 * 60
EXECUTION_TTL =
60 * 60
CONFIG_TTL =
60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage:, collector:, config:, logger: SidekiqVigil.logger, sleeper: ->(seconds) { sleep(seconds) }) ⇒ Reporter

Returns a new instance of Reporter.



14
15
16
17
18
19
20
21
# File 'lib/sidekiq_vigil/reporter.rb', line 14

def initialize(storage:, collector:, config:, logger: SidekiqVigil.logger, sleeper: ->(seconds) { sleep(seconds) })
  @storage = storage
  @collector = collector
  @config = config
  @logger = logger
  @sleeper = sleeper
  @running = false
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



12
13
14
# File 'lib/sidekiq_vigil/reporter.rb', line 12

def thread
  @thread
end

Instance Method Details

#flush_onceObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sidekiq_vigil/reporter.rb', line 39

def flush_once
  snapshot = collector.drain
  flush_snapshot(snapshot)
  report_rss
  storage.hash_write("config_digest", { process_id => config.digest }, ttl: CONFIG_TTL)
  true
rescue StandardError => e
  collector.merge(snapshot) if snapshot
  logger.error("[sidekiq-vigil] reporter flush failed: #{e.class}: #{e.message}")
  false
end

#rss_kbObject



51
52
53
54
55
56
# File 'lib/sidekiq_vigil/reporter.rb', line 51

def rss_kb
  return linux_rss if File.readable?("/proc/self/status")
  return macos_rss if RUBY_PLATFORM.include?("darwin")

  nil
end

#startObject



23
24
25
26
27
28
29
30
# File 'lib/sidekiq_vigil/reporter.rb', line 23

def start
  return thread if thread&.alive?

  @running = true
  @thread = Thread.new { run }
  @thread.name = "sidekiq-vigil-reporter" if @thread.respond_to?(:name=)
  @thread
end

#stopObject



32
33
34
35
36
37
# File 'lib/sidekiq_vigil/reporter.rb', line 32

def stop
  @running = false
  thread&.wakeup if thread&.status == "sleep"
  thread&.join(config.flush_interval + 1)
  @thread = nil
end