splib 1.4.1 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 1.4.2
2
+ * Modify Float#within_delta? to use #between? like any
3
+ reasonable person would
4
+ * Add a priority setting for Splib.exec
5
+ * Add process tracking with Splib.exec so we don't leave
6
+ children laying around if we get shutdown
1
7
  1.4.1
2
8
  * Updates in Monitor to attempt to force
3
9
  threads to stay put
data/Rakefile CHANGED
@@ -15,7 +15,7 @@ spec = Gem::Specification.new do |s|
15
15
  s.name = 'splib'
16
16
  s.author = 'spox'
17
17
  s.email = 'spox@modspox.com'
18
- s.version = '1.4.1'
18
+ s.version = '1.4.2'
19
19
  s.summary = 'Spox Library'
20
20
  s.platform = Gem::Platform::RUBY
21
21
  s.files = %w(LICENSE README.rdoc CHANGELOG Rakefile) + Dir.glob("{bin,lib,spec,test}/**/*")
@@ -2,6 +2,13 @@ require 'timeout'
2
2
 
3
3
  module Splib
4
4
 
5
+ @@processes = []
6
+ Kernel.at_exit{@@processes.each{|p| Process.kill('KILL', pro.pid) }}
7
+
8
+ # Returns current array of running Processes
9
+ def self.running_procs
10
+ @@processes
11
+ end
5
12
  # command:: command string to execute
6
13
  # timeout:: length of time to execute
7
14
  # maxbytes:: maximum number return bytes allowed
@@ -18,17 +25,23 @@ module Splib
18
25
  # command:: command to execute
19
26
  # timeout:: maximum number of seconds to run
20
27
  # maxbytes:: maximum number of result bytes to accept
28
+ # priority:: set priority of the process
21
29
  # Execute a system command (use with care)
22
30
  # This is the normal exec command that is used
23
- def self.standard_exec(command, timeout=10, maxbytes=500)
24
- timout = timeout.to_i
31
+ def self.standard_exec(command, timeout=10, maxbytes=500, priority=nil)
32
+ timeout = timeout.to_i
25
33
  maxbytes = maxbytes.to_i
34
+ priority = priority.to_i
26
35
  output = []
27
36
  pro = nil
28
37
  begin
29
38
  if(timeout > 0)
30
39
  Timeout::timeout(timeout) do
31
40
  pro = IO.popen(command)
41
+ @@processes << pro
42
+ if(priority > 0)
43
+ Process.setpriority(Process::PRIO_PROCESS, pro.pid, priority)
44
+ end
32
45
  until(pro.closed? || pro.eof?)
33
46
  output << pro.getc.chr
34
47
  if(maxbytes > 0 && output.size > maxbytes)
@@ -38,6 +51,7 @@ module Splib
38
51
  end
39
52
  else
40
53
  pro = IO.popen(command)
54
+ @@processes << pro
41
55
  until(pro.closed? || pro.eof?)
42
56
  output << pro.getc.chr
43
57
  if(maxbytes > 0 && output.size > maxbytes)
@@ -48,6 +62,7 @@ module Splib
48
62
  output = output.join('')
49
63
  ensure
50
64
  Process.kill('KILL', pro.pid) if Process.waitpid2(pro.pid, Process::WNOHANG).nil? # make sure the process is dead
65
+ @@processes.delete(pro)
51
66
  end
52
67
  return output
53
68
  end
@@ -58,6 +73,7 @@ module Splib
58
73
  # command:: command to execute
59
74
  # timeout:: maximum number of seconds to run
60
75
  # maxbytes:: maximum number of result bytes to accept
76
+ # priority:: set priority of the process
61
77
  # Execute a system command (use with care)
62
78
  # This is the threaded exec command that is generally used
63
79
  # with JRuby. The regular timeout does not work when executing
@@ -66,6 +82,7 @@ module Splib
66
82
  def self.thread_exec(command, timeout=10, maxbytes=500)
67
83
  timeout = timeout.to_i
68
84
  maxbytes = maxbytes.to_i
85
+ priority = priority.to_i
69
86
  current = Thread.current
70
87
  output = []
71
88
  pro = nil
@@ -73,6 +90,10 @@ module Splib
73
90
  boom = Complete.new
74
91
  begin
75
92
  pro = IO.popen(command)
93
+ @@processes << pro
94
+ if(priority > 0)
95
+ Process.setpriority(Process::PRIO_PROCESS, pro.pid, priority)
96
+ end
76
97
  until(pro.closed? || pro.eof?)
77
98
  output << pro.getc.chr
78
99
  if(maxbytes > 0 && output.size > maxbytes)
@@ -85,13 +106,21 @@ module Splib
85
106
  current.raise boom unless boom.is_a?(Timeout::Error)
86
107
  end
87
108
  begin
88
- timeout > 0 ? sleep(timeout) : sleep
89
- thread.raise Timeout::Error.new
90
- raise Timeout::Error.new
91
- rescue Complete
92
- # do nothing
109
+ begin
110
+ if(timeout > 0)
111
+ thread.join(timeout)
112
+ thread.raise Timeout::Error.new
113
+ raise Timeout::Error.new
114
+ else
115
+ thread.join
116
+ end
117
+ output.join('')
118
+ rescue Complete
119
+ # ignore this exception
120
+ end
93
121
  ensure
94
122
  Process.kill('KILL', pro.pid) unless pro.nil?
123
+ @@processes.delete(pro)
95
124
  end
96
125
  output.join('')
97
126
  end
@@ -7,12 +7,7 @@ module Splib
7
7
  raise ArgumentError.new('Missing required argument: :delta') unless args[:delta]
8
8
  e = args[:expected].to_f
9
9
  d = args[:delta].to_f
10
- mult = 0
11
- if(pos = d.to_s.index('.'))
12
- mult = (d.to_s.length - (pos + 1))
13
- end
14
- mult = 10 ** mult
15
- ((e*mult) - (self*mult)).abs <= (d*mult)
10
+ self.between?(e-d, e+d)
16
11
  end
17
12
  end
18
13
  end
@@ -15,4 +15,16 @@ class ExecTest < Test::Unit::TestCase
15
15
  end
16
16
  assert_equal("test\n", Splib.exec('echo test'))
17
17
  end
18
+
19
+ def test_running
20
+ output = nil
21
+ Thread.new do
22
+ output = Splib.exec('ruby -e \'sleep(0.4); puts "done"\'')
23
+ end
24
+ sleep(0.1)
25
+ assert_equal(1, Splib.running_procs.size)
26
+ sleep(0.5)
27
+ assert_equal("done\n", output)
28
+ assert_equal(0, Splib.running_procs.size)
29
+ end
18
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: splib
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - spox
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-19 00:00:00 -08:00
12
+ date: 2010-01-29 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15