Class: RubyLlmMesh::Mesh::HealthMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm_mesh/mesh/health_monitor.rb

Overview

Pings local LLM peers and marks them healthy/unhealthy for routing.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: RubyLlmMesh.configuration, registry: nil) ⇒ HealthMonitor

Returns a new instance of HealthMonitor.



22
23
24
25
26
27
28
# File 'lib/ruby_llm_mesh/mesh/health_monitor.rb', line 22

def initialize(config: RubyLlmMesh.configuration, registry: nil)
  @config = config
  @registry = registry || PeerRegistry.instance(config: config)
  @mutex = Mutex.new
  @thread = nil
  @stopped = true
end

Class Method Details

.instance(config: RubyLlmMesh.configuration, registry: nil) ⇒ Object



12
13
14
# File 'lib/ruby_llm_mesh/mesh/health_monitor.rb', line 12

def instance(config: RubyLlmMesh.configuration, registry: nil)
  @instance ||= new(config: config, registry: registry)
end

.reset!Object



16
17
18
19
# File 'lib/ruby_llm_mesh/mesh/health_monitor.rb', line 16

def reset!
  @instance&.stop!
  @instance = nil
end

Instance Method Details

#check!(url) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_llm_mesh/mesh/health_monitor.rb', line 34

def check!(url)
  healthy = healthy?(url)
  if healthy
    @registry.mark_healthy(url)
  else
    @registry.mark_unhealthy(url, error: "health check failed")
  end
  healthy
rescue StandardError => e
  @registry.mark_unhealthy(url, error: e.message)
  false
end

#check_all!Object



30
31
32
# File 'lib/ruby_llm_mesh/mesh/health_monitor.rb', line 30

def check_all!
  @registry.all_urls.each { |url| check!(url) }
end

#healthy?(url) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_llm_mesh/mesh/health_monitor.rb', line 47

def healthy?(url)
  base = url.to_s.chomp("/")
  paths = [
    @config.peer_health_path,
    "/v1/models",
    "/api/tags"
  ].uniq

  paths.any? { |path| ping("#{base}#{path}") }
end

#running?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/ruby_llm_mesh/mesh/health_monitor.rb', line 84

def running?
  !@stopped && @thread&.alive?
end

#start!Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_llm_mesh/mesh/health_monitor.rb', line 58

def start!
  return unless @config.peer_discovery_enabled
  return if @thread&.alive?

  @stopped = false
  interval = [@config.peer_health_interval, 1].max
  @thread = Thread.new do
    until @stopped
      begin
        check_all!
      rescue StandardError
        # keep looping
      end
      sleep interval
    end
  end
  @thread.abort_on_exception = false
  @thread
end

#stop!Object



78
79
80
81
82
# File 'lib/ruby_llm_mesh/mesh/health_monitor.rb', line 78

def stop!
  @stopped = true
  @thread&.kill
  @thread = nil
end