subexec 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +2 -2
  2. data/VERSION +1 -1
  3. data/lib/subexec.rb +48 -31
  4. metadata +3 -6
data/README.rdoc CHANGED
@@ -25,5 +25,5 @@ and never returns control back to the original process. Enter Subexec.
25
25
 
26
26
  === Limitations
27
27
 
28
- Supports only Ruby 1.9.
29
-
28
+ Only Ruby 1.9 can spawn non-blocking subprocesses, via Process.spawn.
29
+ So Ruby 1.8 support is sheerly for backwards compatibility.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/lib/subexec.rb CHANGED
@@ -35,52 +35,69 @@ class Subexec
35
35
  attr_accessor :exitstatus
36
36
 
37
37
  def self.run(command, options={})
38
- new(command, options)
38
+ sub = new(command, options)
39
+ sub.run!
40
+ sub
39
41
  end
40
42
 
41
43
  def initialize(command, options={})
42
44
  self.command = command
43
45
  self.timeout = options[:timeout] || -1 # default is to never timeout
44
- run!
45
46
  end
46
47
 
47
48
  def run!
48
- r, w = IO.pipe
49
- self.pid = Process.spawn(command, STDERR=>w, STDOUT=>w)
50
- w.close
51
-
52
- self.timer = Time.now + timeout
53
- timed_out = false
49
+ if timeout > 0 && RUBY_VERSION >= '1.9'
50
+ spawn
51
+ else
52
+ exec
53
+ end
54
+ end
55
+
56
+
57
+ private
58
+
59
+ def spawn
60
+ r, w = IO.pipe
61
+ self.pid = Process.spawn(command, STDERR=>w, STDOUT=>w)
62
+ w.close
54
63
 
55
- loop do
56
- begin
57
- flags = (timeout > 0 ? Process::WUNTRACED|Process::WNOHANG : 0)
58
- ret = Process.waitpid(pid, flags)
59
- rescue Errno::ECHILD
60
- break
64
+ self.timer = Time.now + timeout
65
+ timed_out = false
66
+
67
+ loop do
68
+ begin
69
+ flags = (timeout > 0 ? Process::WUNTRACED|Process::WNOHANG : 0)
70
+ ret = Process.waitpid(pid, flags)
71
+ rescue Errno::ECHILD
72
+ break
73
+ end
74
+
75
+ break if ret == pid
76
+ sleep 0.01
77
+ if Time.now > timer
78
+ timed_out = true
79
+ break
80
+ end
61
81
  end
62
82
 
63
- break if ret == pid
64
- sleep 0.01
65
- if Time.now > timer
66
- timed_out = true
67
- break
83
+ if timed_out
84
+ # The subprocess timed out -- kill it
85
+ Process.kill(9, pid) rescue Errno::ESRCH
86
+ self.exitstatus = nil
87
+ else
88
+ # The subprocess exited on its own
89
+ self.exitstatus = $?.exitstatus
90
+ self.output = r.readlines.join("")
68
91
  end
92
+ r.close
93
+
94
+ self
69
95
  end
70
-
71
- if timed_out
72
- # The subprocess timed out -- kill it
73
- Process.kill(9, pid) rescue Errno::ESRCH
74
- self.exitstatus = nil
75
- else
76
- # The subprocess exited on its own
96
+
97
+ def exec
98
+ self.output = `#{command} 2>&1`
77
99
  self.exitstatus = $?.exitstatus
78
- self.output = r.readlines.join("")
79
100
  end
80
- r.close
81
-
82
- self
83
- end
84
101
 
85
102
  end
86
103
 
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subexec
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 0
9
- - 3
10
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
11
10
  platform: ruby
12
11
  authors:
13
12
  - Peter Kieltyka
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-05-25 00:00:00 -04:00
17
+ date: 2010-07-07 00:00:00 -04:00
19
18
  default_executable:
20
19
  dependencies: []
21
20
 
@@ -46,7 +45,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
46
45
  requirements:
47
46
  - - ">="
48
47
  - !ruby/object:Gem::Version
49
- hash: 3
50
48
  segments:
51
49
  - 0
52
50
  version: "0"
@@ -55,7 +53,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
53
  requirements:
56
54
  - - ">="
57
55
  - !ruby/object:Gem::Version
58
- hash: 3
59
56
  segments:
60
57
  - 0
61
58
  version: "0"