Class: Micron::Runner::LivenessChecker::Ping

Inherits:
Object
  • Object
show all
Includes:
Debug
Defined in:
lib/micron/runner/liveness_checker/ping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debug

#debug

Constructor Details

#initialize(reader, writer, worker) ⇒ Ping

Returns a new instance of Ping.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/micron/runner/liveness_checker/ping.rb', line 14

def initialize(reader, writer, worker)
  @thread = Thread.new(reader, writer, worker) { |reader, writer, worker|

    Thread.current[:name] = "pinger-#{worker.pid}"
    last_pong = Hitimes::Interval.now

    begin
      writer.sync = true

      while true
        begin
          writer.puts "ping"
          debug "sent ping"

          reply = Timeout.timeout(0.1) { reader.readline }

          if "pong\n" == reply then
            last_pong = Hitimes::Interval.now
            debug "got pong response"
          end

        rescue Exception => ex
          if worker.wait_nonblock then
            # process exited, no need to do anything more
            # debug "no pong in #{last_pong.to_f} sec, but process exited"
            break
          end

          debug "no pong received: #{Micron.dump_ex(ex)}"
          if last_pong.to_f > 5.0 then
            debug "no pong in #{last_pong.to_f} sec! Unleash the reaper!!"
            Process.kill(9, worker.pid)
            break
          end
        end

        sleep 0.1
      end # while

    rescue => ex
      debug "caught: #{Micron.dump_ex(ex)}"
    end

    debug "ping thread exiting"
  }
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



12
13
14
# File 'lib/micron/runner/liveness_checker/ping.rb', line 12

def thread
  @thread
end