Module: RightScale
- Defined in:
- lib/right_popen/version.rb,
lib/right_popen.rb,
lib/right_popen/linux/process.rb,
lib/right_popen/linux/utilities.rb,
lib/right_popen/linux/accumulator.rb,
lib/right_popen/linux/right_popen.rb
Overview
Copyright: Copyright © 2011 RightScale, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++
Defined Under Namespace
Modules: InputHandler, PipeHandler, RightPopen, StatusHandler
Class Method Summary collapse
-
.handle_exit(pid, wait_time, handlers, options) ⇒ Object
Wait for process to exit and then call exit handler If no exit detected, double the wait time up to a maximum of 2 seconds.
-
.popen3(options) ⇒ Object
Spawn process to run given command asynchronously, hooking all three standard streams of the child process.
-
.popen3_imp(options) ⇒ Object
Forks process to run given command asynchronously, hooking all three standard streams of the child process.
Class Method Details
.handle_exit(pid, wait_time, handlers, options) ⇒ Object
Wait for process to exit and then call exit handler If no exit detected, double the wait time up to a maximum of 2 seconds
Parameters
- pid(Integer)
-
Process identifier
- wait_time(Fixnum)
-
Amount of time to wait before checking status
- handlers(Array)
-
Handlers for status, stderr, stdout, and stdin
- options(Symbol)
-
Handler to be called when process exits
- options(Object)
-
Object initiating command execution
Return
- true
-
Always return true
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/right_popen/linux/right_popen.rb', line 166 def self.handle_exit(pid, wait_time, handlers, ) EM::Timer.new(wait_time) do if value = Process.waitpid2(pid, Process::WNOHANG) begin ignored, status = value [:target].method([:exit_handler]).call(status) if [:exit_handler] ensure handlers.each { |h| h.drain_and_close } end else handle_exit(pid, [wait_time * 2, 1].min, handlers, ) end end true end |
.popen3(options) ⇒ Object
Spawn process to run given command asynchronously, hooking all three standard streams of the child process.
Streams the command’s stdout and stderr to the given handlers. Time- ordering of bytes sent to stdout and stderr is not preserved.
Calls given exit handler upon command process termination, passing in the resulting Process::Status.
All handlers must be methods exposed by the given target.
Parameters
- options(String or Array)
-
Command to execute, including any arguments as a single string or an array of command and arguments
- options(Hash)
-
Hash of environment variables values keyed by name
- options(String)
-
Input string that will get streamed into child’s process stdin
- options(Object)
-
object defining handler methods to be called, optional (no handlers can be defined if not specified)
- options(String)
-
PID notification handler method name, optional
- options(String)
-
Stdout handler method name, optional
- options(String)
-
Stderr handler method name, optional
- options(String)
-
Exit handler method name, optional
Returns
- true
-
always true
59 60 61 62 63 64 |
# File 'lib/right_popen.rb', line 59 def self.popen3() raise "EventMachine reactor must be started" unless EM.reactor_running? raise "Missing command" unless [:command] raise "Missing target" unless [:target] || ![:stdout_handler] && ![:stderr_handler] && ![:exit_handler] && ![:pid_handler] return RightScale.popen3_imp() end |
.popen3_imp(options) ⇒ Object
Forks process to run given command asynchronously, hooking all three standard streams of the child process.
Parameters
- options(Symbol)
-
Token for pid handler method name.
See RightScale.popen3
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/right_popen/linux/right_popen.rb', line 133 def self.popen3_imp() GC.start # To garbage collect open file descriptors from past executions EM.next_tick do process = RightPopen::Process.new(:environment => [:environment] || {}) process.fork([:command]) handlers = [] handlers << EM.attach(process.status_fd, StatusHandler, process.status_fd) handlers << EM.attach(process.stderr, PipeHandler, process.stderr, [:target], [:stderr_handler]) handlers << EM.attach(process.stdout, PipeHandler, process.stdout, [:target], [:stdout_handler]) handlers << EM.attach(process.stdin, InputHandler, process.stdin, [:input]) [:target].method([:pid_handler]).call(process.pid) if .key? :pid_handler handle_exit(process.pid, 0.1, handlers, ) end true end |