pax_global_header00006660000000000000000000000064121312735420014512gustar00rootroot0000000000000052 comment=2e4d92870d4ad3322d6fca14416f63e6eff55cbc subexec-0.2.3/000077500000000000000000000000001213127354200131525ustar00rootroot00000000000000subexec-0.2.3/.gitignore000066400000000000000000000000151213127354200151360ustar00rootroot00000000000000Gemfile.lock subexec-0.2.3/Gemfile000066400000000000000000000001451213127354200144450ustar00rootroot00000000000000source 'https://rubygems.org' gemspec group :development, :test do gem 'pry' gem 'pry-nav' end subexec-0.2.3/LICENSE000066400000000000000000000020761213127354200141640ustar00rootroot00000000000000MIT LICENSE Copyright (c) 2011 Nulayer Inc. info@nulayer.com 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. subexec-0.2.3/README.md000066400000000000000000000024731213127354200144370ustar00rootroot00000000000000# Subexec by Peter Kieltyka http://github/nulayer/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. Enter Subexec. Tested with MRI 1.9.3, 1.9.2, 1.8.7 Note: Process.spawn seems to be broken with JRuby 1.7.0.dev (as of April 20th, 2012), and so it uses Process.exec instead. ## Usage ```ruby sub = Subexec.run "echo 'hello' && sleep 3", :timeout => 5 puts sub.output # returns: hello puts sub.exitstatus # returns: 0 sub = Subexec.run "echo 'hello' && sleep 3", :timeout => 1 puts sub.output # returns: puts sub.exitstatus # returns:` ``` ## Limitations Only Ruby 1.9 can spawn non-blocking subprocesses, via Process.spawn. So Ruby 1.8 support is sheerly for backwards compatibility. ## Windows Support Limited Windows support is available. We are more than happy to accept patches. However, our tests are run on Unix-like operating systems. Primarily, the intended effect should be that Subexec *works* on Windows, though it may not give many advantages. subexec-0.2.3/Rakefile000066400000000000000000000004051213127354200146160ustar00rootroot00000000000000require 'bundler' Bundler::GemHelper.install_tasks require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) do |spec| spec.pattern = 'spec/**/*_spec.rb' end desc "Benchmark" task :benchmark do sh "ruby benchmark/run.rb" end task :default => :spec subexec-0.2.3/benchmark/000077500000000000000000000000001213127354200151045ustar00rootroot00000000000000subexec-0.2.3/benchmark/run.rb000066400000000000000000000012451213127354200162370ustar00rootroot00000000000000begin require 'bundler' Bundler.setup(:default, :development) rescue LoadError => e # Fall back on doing an unlocked resolve at runtime. $stderr.puts e.message $stderr.puts "Try running `bundle install`" exit! end require 'benchmark' TEST_PROG = File.join(File.expand_path('../spec', File.dirname(__FILE__)), 'helloworld.sh') n = 100 Benchmark.bm(20) do |x| x.report("via Subexec x#{n}") do n.times do sub = Subexec.run "#{TEST_PROG} 0", :timeout => 2 raise if sub.output != "Hello\nWorld\n" end end x.report("via `` x#{n}") do n.times do output = `#{TEST_PROG} 0` raise if output != "Hello\nWorld\n" end end end subexec-0.2.3/lib/000077500000000000000000000000001213127354200137205ustar00rootroot00000000000000subexec-0.2.3/lib/subexec.rb000066400000000000000000000060171213127354200157070ustar00rootroot00000000000000# # Subexec # * by Peter Kieltyka # * http://github/nulayer/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 it 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: class Subexec VERSION = '0.2.3' attr_accessor :pid, :command, :lang, :output, :exitstatus, :timeout, :log_file def self.run(command, options={}) sub = new(command, options) sub.run! sub end def initialize(command, options={}) self.command = command self.lang = options[:lang] || "C" self.timeout = options[:timeout] || -1 # default is to never timeout self.log_file = options[:log_file] self.exitstatus = 0 end def run! if RUBY_VERSION >= '1.9' && RUBY_ENGINE != 'jruby' spawn else exec end end private def spawn # TODO: weak implementation for log_file support. # Ideally, the data would be piped through to both descriptors r, w = IO.pipe log_to_file = !log_file.nil? log_opts = log_to_file ? {[:out, :err] => [log_file, 'a']} : {STDERR=>w, STDOUT=>w} self.pid = Process.spawn({'LANG' => self.lang}, command, log_opts) w.close @timer = Time.now + timeout timed_out = false self.output = '' append_to_output = Proc.new do self.output << r.readlines.join('') unless log_to_file end loop do ret = begin Process.waitpid(pid, Process::WUNTRACED|Process::WNOHANG) rescue Errno::ECHILD break end break if ret == pid append_to_output.call if timeout > 0 && Time.now > @timer timed_out = true break end sleep 0.01 end if timed_out # The subprocess timed out -- kill it Process.kill(9, pid) rescue Errno::ESRCH self.exitstatus = nil else # The subprocess exited on its own self.exitstatus = $?.exitstatus append_to_output.call end r.close self end def exec if !(RUBY_PLATFORM =~ /win32|mswin|mingw/).nil? self.output = `set LANG=#{lang} && #{command} 2>&1` else self.output = `LANG=#{lang} && export LANG && #{command} 2>&1` end self.exitstatus = $?.exitstatus end end subexec-0.2.3/spec/000077500000000000000000000000001213127354200141045ustar00rootroot00000000000000subexec-0.2.3/spec/helloworld.sh000077500000000000000000000000551213127354200166160ustar00rootroot00000000000000#!/bin/sh echo "Hello" sleep $1 echo "World" subexec-0.2.3/spec/spec_helper.rb000066400000000000000000000010411213127354200167160ustar00rootroot00000000000000begin require 'bundler' Bundler.setup(:default, :development) rescue LoadError => e # Fall back on doing an unlocked resolve at runtime. $stderr.puts e.message $stderr.puts "Try running `bundle install`" exit! end $:.unshift(File.dirname(__FILE__)) $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) begin require 'pry' require 'pry-nav' rescue LoadError end require 'subexec' require 'rspec' RSpec.configure do |config| config.mock_with :rspec end TEST_PROG = File.join(File.dirname(__FILE__), 'helloworld.sh') subexec-0.2.3/spec/subexec_spec.rb000066400000000000000000000035071213127354200171060ustar00rootroot00000000000000require 'spec_helper' require 'timeout' describe Subexec do context 'Subexec class' do it 'run helloworld script' do sub = Subexec.run "#{TEST_PROG} 1" sub.output.should == "Hello\nWorld\n" sub.exitstatus.should == 0 end it 'timeout helloworld script' do sub = Subexec.run "#{TEST_PROG} 2", :timeout => 1 if RUBY_VERSION >= '1.9' sub.exitstatus.should == nil else # Ruby 1.8 doesn't support the timeout, so the # subprocess will have to exit on its own sub.exitstatus.should == 0 end end it 'set LANG env variable on request' do original_lang = `echo $LANG` sub = Subexec.run "echo $LANG", :lang => "fr_FR.UTF-8" sub.output.should == "fr_FR.UTF-8\n" sub = Subexec.run "echo $LANG", :lang => "C" sub.output.should == "C\n" sub = Subexec.run "echo $LANG", :lang => "en_US.UTF-8" sub.output.should == "en_US.UTF-8\n" `echo $LANG`.should == original_lang end it 'default LANG to C' do sub = Subexec.run "echo $LANG" sub.output.should == "C\n" end it 'can pass a log_file' do if RUBY_VERSION >= '1.9' require 'tempfile' log_file = Tempfile.new('foo') sub = Subexec.run "#{TEST_PROG} 1", :log_file => log_file.path sub.output.should == '' sub.exitstatus.should == 0 log_file.read.should == "Hello\nWorld\n" log_file.close log_file.unlink end end it 'can handle commands with lengthy outputs and no timeout set' do # See this issue: # http://stackoverflow.com/questions/13829830/ruby-process-spawn-stdout-pipe-buffer-size-limit cmd = "for i in {1..6600}; do echo '123456789'; done" lambda { Timeout::timeout(5) { Subexec.run cmd } }.should_not raise_exception end end end subexec-0.2.3/subexec.gemspec000066400000000000000000000011641213127354200161570ustar00rootroot00000000000000$:.push File.expand_path('../lib', __FILE__) require 'subexec' Gem::Specification.new do |s| s.name = 'subexec' s.version = Subexec::VERSION s.platform = Gem::Platform::RUBY s.summary = "Subexec spawns a subprocess with an optional timeout" s.description = s.summary s.license = 'MIT' s.authors = ["Peter Kieltyka"] s.email = ["peter@nulayer.com"] s.homepage = "http://github.com/nulayer/subexec" s.files = Dir['README.md', 'lib/**/*'] s.require_path = 'lib' s.add_development_dependency('rake') s.add_development_dependency('rspec', ['~> 2.7.0']) end