Class: Subexec
- Inherits:
-
Object
- Object
- Subexec
- Defined in:
- lib/subexec.rb
Overview
Subexec
-
by Peter Kieltyka
Description
Subexec is a simple library that spawns an external command with an optional timeout parameter. It relies on Ruby 1.9’s Process.spawn method. Also, it works with synchronous and asynchronous code.
Useful for libraries that are Ruby wrappers for CLI’s. For example, resizing images with ImageMagick’s mogrify command sometimes stalls and never returns control back to the original process. Subexec executes mogrify and preempts if gets lost.
Usage
# Print hello sub = Subexec.run “echo ‘hello’ && sleep 3”, :timeout => 5 puts sub.output # returns: hello puts sub.exitstatus # returns: 0
# Timeout process after a second sub = Subexec.run “echo ‘hello’ && sleep 3”, :timeout => 1 puts sub.output # returns: puts sub.exitstatus # returns:
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
-
#exitstatus ⇒ Object
Returns the value of attribute exitstatus.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#output ⇒ Object
Returns the value of attribute output.
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#timer ⇒ Object
Returns the value of attribute timer.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(command, options = {}) ⇒ Subexec
constructor
A new instance of Subexec.
- #run! ⇒ Object
Constructor Details
#initialize(command, options = {}) ⇒ Subexec
Returns a new instance of Subexec.
44 45 46 47 48 |
# File 'lib/subexec.rb', line 44 def initialize(command, ={}) self.command = command self.timeout = [:timeout] || -1 # default is to never timeout self.lang = [:lang] || "C" end |
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
31 32 33 |
# File 'lib/subexec.rb', line 31 def command @command end |
#exitstatus ⇒ Object
Returns the value of attribute exitstatus.
35 36 37 |
# File 'lib/subexec.rb', line 35 def exitstatus @exitstatus end |
#lang ⇒ Object
Returns the value of attribute lang.
36 37 38 |
# File 'lib/subexec.rb', line 36 def lang @lang end |
#output ⇒ Object
Returns the value of attribute output.
34 35 36 |
# File 'lib/subexec.rb', line 34 def output @output end |
#pid ⇒ Object
Returns the value of attribute pid.
30 31 32 |
# File 'lib/subexec.rb', line 30 def pid @pid end |
#timeout ⇒ Object
Returns the value of attribute timeout.
32 33 34 |
# File 'lib/subexec.rb', line 32 def timeout @timeout end |
#timer ⇒ Object
Returns the value of attribute timer.
33 34 35 |
# File 'lib/subexec.rb', line 33 def timer @timer end |
Class Method Details
.run(command, options = {}) ⇒ Object
38 39 40 41 42 |
# File 'lib/subexec.rb', line 38 def self.run(command, ={}) sub = new(command, ) sub.run! sub end |
Instance Method Details
#run! ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/subexec.rb', line 50 def run! if timeout > 0 && RUBY_VERSION >= '1.9' spawn else exec end end |