Sha256: 4b6b5c25c23155c4088c6c2b3faa0f3b0fe487e668287d516080e72f4fa3c1c9

Contents?: true

Size: 1.77 KB

Versions: 13

Compression:

Stored size: 1.77 KB

Contents

module Tork
# Wrapper for IO.popen() that automatically re-establishes itself
# whenever the child process terminates extraneously, on its own.
class Bridge

  def initialize command
    @command = command
    connect
  end

  def disconnect
    return unless @guardian.alive?

    # prevent guardian from reconnecting bridge while we disconnect it
    @guardian.exit

    # this should be enough to stop programs that use Tork::Server#loop
    # because their IO.select() loop terminates on the closing of STDIN
    @io.close_write

    # but some programs like tork-herald(1) need to be killed explicitly
    # because they do not follow our convention of exiting on STDIN close
    Process.kill :SIGTERM, @io.pid
    Process.waitpid @io.pid

    # this will block until the child process has exited so we must kill it
    # explicitly (as above) to ensure that this program does not hang here
    @io.close_read

  rescue IOError, SystemCallError
    # IOError happens if the child process' pipes are already closed
    # SystemCallError happens if the child process is already dead
  end

  def reconnect
    disconnect
    connect
  end

  # Allows this object to be passed directly
  # into IO.select() and Tork::Server#tell().
  def to_io
    @io
  end

  # Allows this object to be treated as IO.
  def method_missing *args, &block
    @io.__send__ *args, &block
  end

private

  def connect
    @io = IO.popen(@command, 'r+')

    # automatically reconnect the bridge when the child process terminates
    @guardian = Thread.new do
      Process.waitpid @io.pid
      warn "#{$0}: repairing collapsed bridge: #{@command} #{$?}"
      sleep 1 # avoid spamming the CPU by waiting a bit before reconnecting
      connect # no need to disconnect because child process is already dead
    end
  end

end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
tork-20.0.1 lib/tork/bridge.rb
tork-20.0.0 lib/tork/bridge.rb
tork-19.11.1 lib/tork/bridge.rb
tork-19.10.0 lib/tork/bridge.rb
tork-19.9.0 lib/tork/bridge.rb
tork-19.8.2 lib/tork/bridge.rb
tork-19.8.1 lib/tork/bridge.rb
tork-19.8.0 lib/tork/bridge.rb
tork-19.7.0 lib/tork/bridge.rb
tork-19.6.1 lib/tork/bridge.rb
tork-19.6.0 lib/tork/bridge.rb
tork-19.5.1 lib/tork/bridge.rb
tork-19.5.0 lib/tork/bridge.rb