Class: Bosh::Monitor::Runner

Inherits:
Object
  • Object
show all
Includes:
YamlHelper
Defined in:
lib/bosh/monitor/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from YamlHelper

#load_yaml_file

Constructor Details

#initialize(config_file) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
15
16
17
18
# File 'lib/bosh/monitor/runner.rb', line 9

def initialize(config_file)
  Bhm.config = load_yaml_file(config_file)

  @logger        = Bhm.logger
  @director      = Bhm.director
  @intervals     = Bhm.intervals
  @mbus          = Bhm.mbus
  @agent_manager = Bhm.agent_manager
  EM.threadpool_size = Bhm.em_threadpool_size
end

Class Method Details

.run(config_file) ⇒ Object



5
6
7
# File 'lib/bosh/monitor/runner.rb', line 5

def self.run(config_file)
  new(config_file).run
end

Instance Method Details

#analyze_agentsObject



101
102
103
104
105
# File 'lib/bosh/monitor/runner.rb', line 101

def analyze_agents
  # N.B. Yes, his will block event loop,
  # possibly consider deferring
  @agent_manager.analyze_agents
end

#connect_to_mbusObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bosh/monitor/runner.rb', line 62

def connect_to_mbus
  NATS.on_error do |e|
    unless @shutting_down
      if e.kind_of?(NATS::ConnectError)
        handle_em_error(e)
      else
        log_exception(e)
      end
    end
  end

  nats_client_options = {
    :uri       => @mbus.endpoint,
    :user      => @mbus.user,
    :pass      => @mbus.password,
    :autostart => false
  }

  Bhm.nats = NATS.connect(nats_client_options) do
    @logger.info("Connected to NATS at '#{@mbus.endpoint}'")
  end
end

#log_statsObject



55
56
57
58
59
60
# File 'lib/bosh/monitor/runner.rb', line 55

def log_stats
  n_deployments = pluralize(@agent_manager.deployments_count, "deployment")
  n_agents = pluralize(@agent_manager.agents_count, "agent")
  @logger.info("Managing #{n_deployments}, #{n_agents}")
  @logger.info("Agent heartbeats received = %s" % [ @agent_manager.heartbeats_received ])
end

#poll_directorObject



96
97
98
99
# File 'lib/bosh/monitor/runner.rb', line 96

def poll_director
  @logger.debug "Getting deployments from director..."
  Fiber.new { fetch_deployments }.resume
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bosh/monitor/runner.rb', line 20

def run
  @logger.info("HealthMonitor starting...")
  EM.kqueue if EM.kqueue?
  EM.epoll if EM.epoll?

  EM.error_handler { |e| handle_em_error(e) }

  EM.run do
    connect_to_mbus
    @director_monitor = DirectorMonitor.new(Bhm)
    @director_monitor.subscribe
    @agent_manager.setup_events
    setup_timers
    start_http_server
    @logger.info "BOSH HealthMonitor #{Bhm::VERSION} is running..."
  end
end

#setup_timersObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bosh/monitor/runner.rb', line 43

def setup_timers
  EM.schedule do
    poll_director
    EM.add_periodic_timer(@intervals.poll_director) { poll_director }
    EM.add_periodic_timer(@intervals.log_stats) { log_stats }

    EM.add_timer(@intervals.poll_grace_period) do
      EM.add_periodic_timer(@intervals.analyze_agents) { analyze_agents }
    end
  end
end

#start_http_serverObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/bosh/monitor/runner.rb', line 85

def start_http_server
  @logger.info "HTTP server is starting on port #{Bhm.http_port}..."
  @http_server = Thin::Server.new("127.0.0.1", Bhm.http_port, :signals => false) do
    Thin::Logging.silent = true
    map "/" do
      run Bhm::ApiController.new
    end
  end
  @http_server.start!
end

#stopObject



38
39
40
41
# File 'lib/bosh/monitor/runner.rb', line 38

def stop
  @logger.info("HealthMonitor shutting down...")
  @http_server.stop! if @http_server
end