Module: ProcessWatcher
- Defined in:
- lib/process_watcher.rb,
lib/process_watcher/version.rb,
lib/process_watcher/watcher.rb,
lib/process_watcher/linux/process_monitor.rb,
lib/process_watcher/win32/process_monitor.rb
Overview
– Copyright: Copyright © 2010 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
Classes: NonzeroExitCode, ProcessMonitor, TimeoutError, TooMuchSpaceError, WatchStatus, Watcher
Constant Summary collapse
- VERSION =
"0.4"
Class Method Summary collapse
-
.run(cmd, *args) ⇒ Object
Spawn given process, wait for it to complete, and return its output and the exit status of the process.
-
.watch(command, args, dir = '.', max_bytes = -1,, max_seconds = -1)) ⇒ Object
Watch command, respecting
max_bytes
andmax_seconds
.
Class Method Details
.run(cmd, *args) ⇒ Object
Spawn given process, wait for it to complete, and return its output and the exit status of the process. Functions similarly to the backtick operator, only it avoids invoking the command interpreter under operating systems that support fork-and-exec.
This method accepts a variable number of parameters; the first param is always the command to run; successive parameters are command-line arguments for the process.
Parameters
- cmd(String)
-
Name of the command to run
- arg1(String)
-
Optional, first command-line argumument
- arg2(String)
-
Optional, first command-line argumument
…
- argN(String)
-
Optional, Nth command-line argumument
Return
- output(String)
-
The process’s output
- status(Process::Status)
-
The process’s exit status
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/process_watcher.rb', line 140 def self.run(cmd, *args) pm = ProcessWatcher::ProcessMonitor.new status = nil output = StringIO.new pm.spawn(cmd, *args) do || output << [:output] if [:output] status = [:exit_status] if [:exit_status] end pm.cleanup output.close output = output.string return [output, status] end |
.watch(command, args, dir = '.', max_bytes = -1,, max_seconds = -1)) ⇒ Object
Watch command, respecting max_bytes
and max_seconds
. Returns the output of the command, with STDOUT and STDERR interleaved, if the command completed successfully. Will raise one of three errors in exceptional cases:
- TimeoutError
-
if the process takes longer than max_seconds to run
- TooMuchSpaceError
-
if the process consumes too much space in
dir
- NonzeroExitCode
-
if the process exits, but with a nonzero exit code
This method accepts a block. If that block exists, it is called at most twice with three arguments, phase
, command
and exception
. The block is called once with phase
set to :begin, command
describing the command run, and exception
being nil. If execution completes normally, the block is called with phase
set to :commit, command
again describing the command, and exception
nil. If an abnormal termination occurs (for example, running out of space), the block is called with phase
set to :abort, command
again describing the command and exception
set to the exception in question.
Parameters
- command(String)
-
command to run
- args(Array)
-
arguments for the command
- dir(String)
-
directory to monitor (defaults to ‘.’)
- max_bytes(Integer)
-
maximum number of bytes to permit (defaults to no restriction)
- max_seconds(Integer)
-
maximum number of seconds to permit to run (defaults to no restriction)
Block parameters
- phase(Keyword)
-
one of :begin, :commit, or :abort
- command(String)
-
description of what command is being run and where it is being run
- exception(Exception)
-
if non nil, the exception raised during the running of the subprocess
Returns
- String
-
output of command
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/process_watcher.rb', line 97 def self.watch(command, args, dir='.', max_bytes=-1, max_seconds=-1) # :yields: phase, command, exception watcher = ProcessWatcher::Watcher.new(max_bytes, max_seconds) text = "in #{dir}, running #{command} #{Shellwords.join(args)}" block_given? and yield :begin, text, nil begin result = watcher.launch_and_watch(command, args, dir) if result.status == :timeout raise TimeoutError, "command took too much time" elsif result.status == :size_exceeded raise TooMuchSpaceError, "command took too much space" elsif result.exit_code != 0 raise NonzeroExitCode.new(result.exit_code, result.output), "nonzero exit code" else result.output end rescue => e block_given? and yield :abort, text, e raise else block_given? and yield :commit, text, nil result.output end end |