Class: Tork::Bridge

Inherits:
Object
  • Object
show all
Defined in:
lib/tork/bridge.rb

Overview

whenever the child process terminates extraneously, on its own.

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Bridge

Returns a new instance of Bridge.



6
7
8
9
# File 'lib/tork/bridge.rb', line 6

def initialize command
  @command = command
  connect
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object

Allows this object to be treated as IO.



47
48
49
# File 'lib/tork/bridge.rb', line 47

def method_missing *args, &block
  @io.__send__ *args, &block
end

Instance Method Details

#disconnectObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tork/bridge.rb', line 11

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

#reconnectObject



35
36
37
38
# File 'lib/tork/bridge.rb', line 35

def reconnect
  disconnect
  connect
end

#to_ioObject

Allows this object to be passed directly into IO.select() and Tork::Server#tell().



42
43
44
# File 'lib/tork/bridge.rb', line 42

def to_io
  @io
end