Module: CpuInspectCore

Defined in:
lib/cpu_inspect_core.rb,
lib/cpu_inspect_core/store.rb,
lib/cpu_inspect_core/monitor.rb,
lib/cpu_inspect_core/railtie.rb,
lib/cpu_inspect_core/version.rb,
lib/cpu_inspect_core/renderer.rb,
lib/cpu_inspect_core/collector.rb,
lib/cpu_inspect_core/log_writer.rb,
lib/cpu_inspect_core/configuration.rb,
lib/cpu_inspect_core/backends/file_backend.rb,
lib/cpu_inspect_core/backends/redis_backend.rb,
lib/cpu_inspect_core/collectors/linux_collector.rb,
lib/cpu_inspect_core/collectors/macos_collector.rb

Defined Under Namespace

Modules: Backends, Collectors Classes: Collector, Configuration, LogWriter, Monitor, Railtie, Renderer, Store

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.configurationObject



17
18
19
# File 'lib/cpu_inspect_core.rb', line 17

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



21
22
23
# File 'lib/cpu_inspect_core.rb', line 21

def configure
  yield configuration
end

.running?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/cpu_inspect_core.rb', line 53

def running?
  @monitor&.running? || false
end

.startObject

Start background monitoring. Idempotent — safe to call multiple times.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cpu_inspect_core.rb', line 26

def start
  return @monitor if @monitor&.running?

  collector = [
    Collectors::LinuxCollector.new,
    Collectors::MacosCollector.new
  ].find(&:available?)

  raise 'CpuInspectCore: no supported collector on this platform' unless collector

  @backend = case configuration.backend
             when :redis then Backends::RedisBackend.new(configuration)
             else             Backends::FileBackend.new(configuration)
             end

  @monitor = Monitor.new(
    collector: collector,
    backend: @backend,
    config: configuration
  )
  @monitor.start
end

.statusObject

Print a per-dyno CPU snapshot to stdout.

File backend → shows only the local process (one block). Redis backend → shows every live dyno (one block each), perfect for

a `heroku run rails runner "CpuInspectCore.status"`.

Use from Rails console: CpuInspectCore.status Use from bash: bundle exec cpu-inspect-core –once



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cpu_inspect_core.rb', line 65

def status
  unless running?
    puts 'CpuInspectCore is not running. Call CpuInspectCore.start first.'
    return
  end

  dynos = @backend&.all_dynos
  if dynos.nil? || dynos.empty?
    puts "Waiting for first sample (interval: #{configuration.interval}s)…"
    return
  end

  renderer  = Renderer.new(configuration)
  last_dyno = dynos.keys.last

  dynos.each do |dyno, sample|
    puts "=== #{dyno} @ #{sample[:ts]} ==="
    puts renderer.render(sample[:cores])
    puts unless dyno == last_dyno
  end
end

.stopObject



49
50
51
# File 'lib/cpu_inspect_core.rb', line 49

def stop
  @monitor&.stop
end

.tail(lines: 20) ⇒ Object

Tail the raw local log file (per-dyno, regardless of backend).



88
89
90
91
92
93
94
95
96
# File 'lib/cpu_inspect_core.rb', line 88

def tail(lines: 20)
  path = configuration.log_path
  unless File.exist?(path)
    puts "Log file not found: #{path}"
    return
  end

  File.readlines(path, chomp: true).last(lines).each { |l| puts l }
end