Sha256: 10733042e1595f49b198debe6408d07884d9aad9eb16d63c17b3bb6e9d20c6db

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

require "timeout"

module Micron
  class Runner
    class LivenessChecker

      class Ping

        include Debug

        attr_reader :thread

        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

      end # Ping

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
micron-0.5.1 lib/micron/runner/liveness_checker/ping.rb
micron-0.5.0 lib/micron/runner/liveness_checker/ping.rb