Class: Cod::Process
- Inherits:
-
Object
- Object
- Cod::Process
- Defined in:
- lib/cod/process.rb
Overview
Instance Attribute Summary collapse
-
#pid ⇒ Number
readonly
The pid of the process that was spawned.
Instance Method Summary collapse
-
#channel ⇒ Cod::Pipe
Returns the cod channel associated with this process.
-
#initialize(command, serializer = nil) ⇒ Process
constructor
Constructs a process object and runs the command.
-
#kill ⇒ void
Stops the process unilaterally.
- #run(command) ⇒ Object
-
#terminate ⇒ void
Asks the process to terminate by closing its stanard input.
-
#wait ⇒ Number?
Waits for the process to terminate and returns its exit value.
Constructor Details
#initialize(command, serializer = nil) ⇒ Process
Constructs a process object and runs the command.
24 25 26 27 28 |
# File 'lib/cod/process.rb', line 24 def initialize(command, serializer=nil) @serializer = serializer || SimpleSerializer.new run(command) end |
Instance Attribute Details
#pid ⇒ Number (readonly)
The pid of the process that was spawned.
19 20 21 |
# File 'lib/cod/process.rb', line 19 def pid @pid end |
Instance Method Details
#channel ⇒ Cod::Pipe
Returns the cod channel associated with this process. The channel will have the process’ standard output bound to its #get (input), and the process’ standard input will be bound to #put (output).
Note that when the process exits and all communication has been read from the channel, it will probably raise a Cod::ConnectionLost error.
57 58 59 |
# File 'lib/cod/process.rb', line 57 def channel @pipe end |
#kill ⇒ void
This method returns an undefined value.
Stops the process unilaterally.
65 66 67 68 |
# File 'lib/cod/process.rb', line 65 def kill terminate ::Process.kill :TERM, @pid end |
#run(command) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cod/process.rb', line 31 def run(command) @pipe = Cod.bidir(@serializer) @pid = ::Process.spawn(command, :in => @pipe.other, :out => @pipe.other) # Close the end we just dedicated to the process we've spawned. # As a consequence, when the process exists, we'll get a read error # on our side of the pipe (@pipe.socket, #put, #get) @pipe.other.close end |
#terminate ⇒ void
This method returns an undefined value.
Asks the process to terminate by closing its stanard input. This normally closes down the process, but no guarantees are made.
75 76 77 |
# File 'lib/cod/process.rb', line 75 def terminate @pipe.socket.close_write end |
#wait ⇒ Number?
Waits for the process to terminate and returns its exit value. May return nil, in which case someone else already reaped the process.
84 85 86 87 |
# File 'lib/cod/process.rb', line 84 def wait ::Process.wait(@pid) rescue Errno::ECHILD end |