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

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, options)
  EM::Timer.new(wait_time) do
    if value = Process.waitpid2(pid, Process::WNOHANG)
      begin
        ignored, status = value
        options[:target].method(options[:exit_handler]).call(status) if options[:exit_handler]
      ensure
        handlers.each { |h| h.drain_and_close }
      end
    else
      handle_exit(pid, [wait_time * 2, 1].min, handlers, options)
    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(options)
  raise "EventMachine reactor must be started" unless EM.reactor_running?
  raise "Missing command" unless options[:command]
  raise "Missing target" unless options[:target] || !options[:stdout_handler] && !options[:stderr_handler] && !options[:exit_handler] && !options[:pid_handler]
  return RightScale.popen3_imp(options)
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(options)
  GC.start # To garbage collect open file descriptors from past executions
  EM.next_tick do
    process = RightPopen::Process.new(:environment => options[:environment] || {})
    process.fork(options[:command])

    handlers = []
    handlers << EM.attach(process.status_fd, StatusHandler, process.status_fd)
    handlers << EM.attach(process.stderr, PipeHandler, process.stderr, options[:target],
                          options[:stderr_handler])
    handlers << EM.attach(process.stdout, PipeHandler, process.stdout, options[:target],
                          options[:stdout_handler])
    handlers << EM.attach(process.stdin, InputHandler, process.stdin, options[:input])

    options[:target].method(options[:pid_handler]).call(process.pid) if options.key? :pid_handler

    handle_exit(process.pid, 0.1, handlers, options)
  end
  true
end