Class: Abalone::Watchdog

Inherits:
Object
  • Object
show all
Defined in:
lib/abalone/watchdog.rb

Instance Method Summary collapse

Constructor Details

#initialize(period, options) ⇒ Watchdog

Returns a new instance of Watchdog.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/abalone/watchdog.rb', line 2

def initialize(period, options)
  raise 'Watchdog needs a port to be set' unless options.include? :port
  raise 'Watchdog needs a logger to be set' unless options.include? :logger
  logger = options[:logger]
  pid    = Process.pid
  socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0).tap do |socket|
    socket.connect(Socket.pack_sockaddr_un(ENV['NOTIFY_SOCKET']))
    socket.close_on_exec = true
  end

  Thread.new do
    require 'net/http'
    sleep 5 # give the service time to wake up

    logger.warn "Starting watchdog with period #{period}"
    loop do
      http = Net::HTTP.new('localhost', options[:port])
      http.open_timeout = 1
      http.read_timeout = 1
      begin
        http.start
        http.request_get('/heartbeat/ping') do |res|
          logger.debug "Heartbeat response: #{res.read_body}"
          #system('systemd-notify WATCHDOG=1')
          socket.write("WATCHDOG=1\nMAINPID=#{pid}")
        end
      rescue => e
        logger.warn 'Abalone service failed heartbeat check!'
        logger.debug e.message
      end

      sleep period
    end
  end
end