Class: RailsOrbit::Kamal::Poller

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_orbit/kamal/poller.rb

Constant Summary collapse

POLL_INTERVAL =
30

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
# File 'lib/rails_orbit/kamal/poller.rb', line 8

def self.available?
  return @available if defined?(@available)
  @available = begin
    require "sshkit"
    require "sshkit/dsl"
    true
  rescue LoadError
    false
  end
end

.start!Object



19
20
21
22
23
24
25
# File 'lib/rails_orbit/kamal/poller.rb', line 19

def self.start!
  unless available?
    Rails.logger.warn "[rails_orbit] Kamal polling requires the sshkit gem. Add it to your Gemfile."
    return
  end
  new.schedule
end

Instance Method Details

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rails_orbit/kamal/poller.rb', line 39

def run
  config   = RailsOrbit.configuration
  servers  = ConfigReader.servers
  user     = ConfigReader.ssh_user
  key_path = config.kamal_ssh_key_path || ENV["ORBIT_SSH_KEY_PATH"]

  unless key_path
    Rails.logger.error "[rails_orbit] kamal_ssh_key_path must be set when kamal_enabled is true"
    return
  end

  collector = StatsCollector.new
  servers.each do |host|
    stats = collector.collect(host: host, user: user, ssh_key_path: key_path)
    stats.each do |s|
      dimension = "#{s[:host]}/#{s[:container]}"
      MetricWriter.write(key: "kamal.cpu_pct", value: s[:cpu_pct], dimension: dimension)
      MetricWriter.write(key: "kamal.mem_pct", value: s[:mem_pct], dimension: dimension)
    end
  end
rescue => e
  Rails.logger.error "[rails_orbit] Kamal poller error: #{e.class} - #{e.message}"
end

#scheduleObject



27
28
29
30
31
32
33
# File 'lib/rails_orbit/kamal/poller.rb', line 27

def schedule
  @task = Concurrent::TimerTask.new(
    execution_interval: POLL_INTERVAL,
    run_now: true
  ) { run }
  @task.execute
end

#stopObject



35
36
37
# File 'lib/rails_orbit/kamal/poller.rb', line 35

def stop
  @task&.shutdown
end