Class: Subexec

Inherits:
Object
  • Object
show all
Defined in:
lib/subexec.rb

Overview

Subexec

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

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
  self.command  = command
  self.timeout  = options[:timeout] || -1 # default is to never timeout
  self.lang     = options[:lang] || "C"
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



31
32
33
# File 'lib/subexec.rb', line 31

def command
  @command
end

#exitstatusObject

Returns the value of attribute exitstatus.



35
36
37
# File 'lib/subexec.rb', line 35

def exitstatus
  @exitstatus
end

#langObject

Returns the value of attribute lang.



36
37
38
# File 'lib/subexec.rb', line 36

def lang
  @lang
end

#outputObject

Returns the value of attribute output.



34
35
36
# File 'lib/subexec.rb', line 34

def output
  @output
end

#pidObject

Returns the value of attribute pid.



30
31
32
# File 'lib/subexec.rb', line 30

def pid
  @pid
end

#timeoutObject

Returns the value of attribute timeout.



32
33
34
# File 'lib/subexec.rb', line 32

def timeout
  @timeout
end

#timerObject

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, options={})
  sub = new(command, options)
  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