Class: ChildProcess::AbstractProcess
- Inherits:
-
Object
- Object
- ChildProcess::AbstractProcess
- Defined in:
- lib/childprocess/abstract_process.rb
Direct Known Subclasses
Constant Summary collapse
- POLL_INTERVAL =
0.1
Instance Attribute Summary collapse
-
#detach ⇒ Object
Set this to true if you do not care about when or if the process quits.
-
#duplex ⇒ Object
Set this to true if you want to write to the process’ stdin (process.io.stdin).
-
#environment ⇒ Object
readonly
Modify the child’s environment variables.
-
#exit_code ⇒ Object
readonly
Returns the value of attribute exit_code.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Is this process running?.
-
#crashed? ⇒ Boolean
Returns true if the process has exited and the exit code was not 0.
-
#exited? ⇒ Boolean
Did the process exit?.
-
#initialize(args) ⇒ AbstractProcess
constructor
private
Create a new process with the given args.
-
#io ⇒ Object
Returns a ChildProcess::AbstractIO subclass to configure the child’s IO streams.
-
#pid ⇒ Fixnum
The pid of the process after it has started.
-
#poll_for_exit(timeout) ⇒ Object
Wait for the process to exit, raising a ChildProcess::TimeoutError if the timeout expires.
-
#start ⇒ AbstractProcess
Launch the child process.
-
#stop(timeout = 3) ⇒ Object
Forcibly terminate the process, using increasingly harsher methods if possible.
-
#wait ⇒ Fixnum
Block until the process has been terminated.
Constructor Details
#initialize(args) ⇒ AbstractProcess
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new process with the given args.
29 30 31 32 33 34 35 36 37 |
# File 'lib/childprocess/abstract_process.rb', line 29 def initialize(args) @args = args @started = false @exit_code = nil @io = nil @detach = false @duplex = false @environment = {} end |
Instance Attribute Details
#detach ⇒ Object
Set this to true if you do not care about when or if the process quits.
10 11 12 |
# File 'lib/childprocess/abstract_process.rb', line 10 def detach @detach end |
#duplex ⇒ Object
Set this to true if you want to write to the process’ stdin (process.io.stdin)
15 16 17 |
# File 'lib/childprocess/abstract_process.rb', line 15 def duplex @duplex end |
#environment ⇒ Object (readonly)
Modify the child’s environment variables
20 21 22 |
# File 'lib/childprocess/abstract_process.rb', line 20 def environment @environment end |
#exit_code ⇒ Object (readonly)
Returns the value of attribute exit_code.
5 6 7 |
# File 'lib/childprocess/abstract_process.rb', line 5 def exit_code @exit_code end |
Instance Method Details
#alive? ⇒ Boolean
Is this process running?
104 105 106 |
# File 'lib/childprocess/abstract_process.rb', line 104 def alive? started? && !exited? end |
#crashed? ⇒ Boolean
Returns true if the process has exited and the exit code was not 0.
114 115 116 |
# File 'lib/childprocess/abstract_process.rb', line 114 def crashed? exited? && @exit_code != 0 end |
#exited? ⇒ Boolean
Did the process exit?
94 95 96 |
# File 'lib/childprocess/abstract_process.rb', line 94 def exited? raise SubclassResponsibility, "exited?" end |
#io ⇒ Object
Returns a ChildProcess::AbstractIO subclass to configure the child’s IO streams.
43 44 45 |
# File 'lib/childprocess/abstract_process.rb', line 43 def io raise SubclassResponsibility, "io" end |
#pid ⇒ Fixnum
Returns the pid of the process after it has started.
51 52 53 |
# File 'lib/childprocess/abstract_process.rb', line 51 def pid raise SubclassResponsibility, "pid" end |
#poll_for_exit(timeout) ⇒ Object
Wait for the process to exit, raising a ChildProcess::TimeoutError if the timeout expires.
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/childprocess/abstract_process.rb', line 123 def poll_for_exit(timeout) log "polling #{timeout} seconds for exit" end_time = Time.now + timeout until (ok = exited?) || Time.now > end_time sleep POLL_INTERVAL end unless ok raise TimeoutError, "process still alive after #{timeout} seconds" end end |
#start ⇒ AbstractProcess
Launch the child process
61 62 63 64 65 66 |
# File 'lib/childprocess/abstract_process.rb', line 61 def start launch_process @started = true self end |
#stop(timeout = 3) ⇒ Object
Forcibly terminate the process, using increasingly harsher methods if possible.
74 75 76 |
# File 'lib/childprocess/abstract_process.rb', line 74 def stop(timeout = 3) raise SubclassResponsibility, "stop" end |
#wait ⇒ Fixnum
Block until the process has been terminated.
84 85 86 |
# File 'lib/childprocess/abstract_process.rb', line 84 def wait raise SubclassResponsibility, "wait" end |