Class: Aggkit::ChildProcess::AbstractProcess
- Inherits:
-
Object
- Object
- Aggkit::ChildProcess::AbstractProcess
- Defined in:
- lib/aggkit/childprocess/abstract_process.rb
Direct Known Subclasses
Constant Summary collapse
- POLL_INTERVAL =
0.1
Instance Attribute Summary collapse
-
#cwd ⇒ Object
Set the child’s current working directory.
-
#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.
-
#leader ⇒ Object
Set this to true to make the child process the leader of a new process group.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
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 ⇒ Integer
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.
-
#started? ⇒ Boolean
Has the process started?.
-
#stop(timeout = 3) ⇒ Object
Forcibly terminate the process, using increasingly harsher methods if possible.
-
#wait ⇒ Integer
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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 46 def initialize(args) unless args.all? { |e| e.kind_of?(String) } raise ArgumentError, "all arguments must be String: #{args.inspect}" end @args = args @started = false @exit_code = nil @io = nil @cwd = nil @detach = false @duplex = false @leader = false @environment = {} end |
Instance Attribute Details
#cwd ⇒ Object
Set the child’s current working directory.
29 30 31 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 29 def cwd @cwd end |
#detach ⇒ Object
Set this to true if you do not care about when or if the process quits.
14 15 16 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 14 def detach @detach end |
#duplex ⇒ Object
Set this to true if you want to write to the process’ stdin (process.io.stdin)
19 20 21 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 19 def duplex @duplex end |
#environment ⇒ Object (readonly)
Modify the child’s environment variables
24 25 26 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 24 def environment @environment end |
#exit_code ⇒ Object (readonly)
Returns the value of attribute exit_code.
7 8 9 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 7 def exit_code @exit_code end |
#leader ⇒ Object
Set this to true to make the child process the leader of a new process group
This can be used to make sure that all grandchildren are killed when the child process dies.
37 38 39 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 37 def leader @leader end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
9 10 11 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 9 def status @status end |
Instance Method Details
#alive? ⇒ Boolean
Is this process running?
137 138 139 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 137 def alive? started? && !exited? end |
#crashed? ⇒ Boolean
Returns true if the process has exited and the exit code was not 0.
147 148 149 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 147 def crashed? exited? && @exit_code != 0 end |
#exited? ⇒ Boolean
Did the process exit?
117 118 119 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 117 def exited? raise SubclassResponsibility, "exited?" end |
#io ⇒ Object
Returns a ChildProcess::AbstractIO subclass to configure the child’s IO streams.
66 67 68 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 66 def io raise SubclassResponsibility, "io" end |
#pid ⇒ Integer
Returns the pid of the process after it has started.
74 75 76 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 74 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.
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 156 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
84 85 86 87 88 89 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 84 def start launch_process @started = true self end |
#started? ⇒ Boolean
Has the process started?
127 128 129 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 127 def started? @started end |
#stop(timeout = 3) ⇒ Object
Forcibly terminate the process, using increasingly harsher methods if possible.
97 98 99 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 97 def stop(timeout = 3) raise SubclassResponsibility, "stop" end |
#wait ⇒ Integer
Block until the process has been terminated.
107 108 109 |
# File 'lib/aggkit/childprocess/abstract_process.rb', line 107 def wait raise SubclassResponsibility, "wait" end |