execute 0.1.52 → 0.1.54

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cmd.rb +54 -9
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6a15d8efb84c0bca96acd3832af4c228aa810ea
4
- data.tar.gz: 9991f75eade05a6952209962673fb70db38c7dca
3
+ metadata.gz: 5a81a615ca56e62d836536bba6830d919528f28d
4
+ data.tar.gz: 5c9e5fef10d7dc9ee3525ef034c38036d8b7da86
5
5
  SHA512:
6
- metadata.gz: 3d965bdfefdb32473e7f59d02b79ac2b09801f9197a0de65aa1cbb3c6e8f3338cdbdeaecd7ca3d59273a060466df9782b39700c77b85c20a64c83372a3228d56
7
- data.tar.gz: d5d35ed94ed6a13428434913bd13ccf9fd361ca5e7b3bce4c16a9e41485ce0b65df9b79e477612ebe750f14dc16c366d70ad040534bbaa1e71b1eaf778f12920
6
+ metadata.gz: ffa7fbb1f77e5ab512bafc105f5ddc830f860cad7c4acbf5cf83a84f49b91899992e7c7544f3f552da857f716a88b2347db54ac182ac82ae2bd2016e7a82fdf6
7
+ data.tar.gz: 69c962093a87bf7dbed0ec1a1cc7ecde6d3664449c795b67451ce04f5b69a5fbd29e431640a5cb36a0948714c3fa7d13bd3295de466fb9c4707813307c3f46bf
data/lib/cmd.rb CHANGED
@@ -48,8 +48,9 @@ class CMD < Hash
48
48
  end
49
49
 
50
50
  puts self[:command] if(self[:echo_command] || self[:debug])
51
- system
52
-
51
+ #call_popen
52
+ call_capture
53
+
53
54
  if(self[:debug])
54
55
  puts "command: #{self[:command]}"
55
56
  puts "output: #{self[:output]}"
@@ -57,7 +58,7 @@ class CMD < Hash
57
58
  puts "exit_code: #{self[:exit_code]}"
58
59
  end
59
60
 
60
- raise TimeoutError.new("Commnad '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error])
61
+ raise TimeoutError.new("Command '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error])
61
62
 
62
63
  if((self[:exit_code] != 0) && !self[:ignore_exit_code])
63
64
  exception_text = "Exit code: #{self[:exit_code]}"
@@ -67,7 +68,7 @@ class CMD < Hash
67
68
  end
68
69
  end
69
70
 
70
- def system
71
+ def call_popen
71
72
  begin
72
73
  output = ''
73
74
  error = ''
@@ -120,13 +121,57 @@ class CMD < Hash
120
121
  self[:exit_code]=1 unless(self[:exit_code].nil? || (self[:exit_code] == 0))
121
122
  end
122
123
  end
124
+ def call_capture
125
+ elapsed_time = nil
126
+ begin
127
+ if(key?(:timeout))
128
+ start_time = Time.now
129
+ Thread.new do
130
+ while !key?(:exit_code) do
131
+ sleep(0.1)
132
+ if((Time.now - start_time).to_f > self[:timeout])
133
+ self[:timed_out] = true
134
+ interrupt
135
+ sleep(0.1)
136
+ break
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ self[:output] = self[:error] = ''
143
+ elapsed_time = Benchmark.realtime do
144
+ self[:output], self[:error], status = Open3.capture3(self[:command])
145
+ self[:exit_code] = status.to_i
146
+ end
147
+
148
+ raise TimeoutError.new("Command '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error])
149
+ rescue Exception => e
150
+ self[:error] = "#{self[:error]}\nException: #{e.to_s}"
151
+ self[:exit_code]=1 unless(!self[:exit_code].nil? || (self[:exit_code] == 0))
152
+ ensure
153
+ self[:elapsed_time] = elapsed_time unless(elapsed_time.nil?)
154
+ end
155
+ end
156
+ def command_pid
157
+ Sys::ProcTable.ps do |p|
158
+ if(p.ppid == $$)
159
+ return self[:pid] = p.pid
160
+ end
161
+ end
162
+
163
+ raise "Failed to find child process for command: '#{self[:command]}'"
164
+ end
123
165
  def interrupt
124
- process = get_child_processes(self[:pid])
166
+ self[:pid] = command_pid unless(key?(:pid))
167
+ raise "Do not have a process id for #{self[:pid]}" unless(key?(:pid))
168
+ processes = get_child_processes(self[:pid])
125
169
 
126
- Sys::ProcTable.ps { |p| process << p if(p.pid == self[:pid]) }
127
-
128
- process.each { |p| Process.kill(self[:timeout_signal],p.pid) unless(Sys::ProcTable.ps(p.pid).nil?) }
129
- Process.waitpid(self[:pid]) unless(Sys::ProcTable.ps(self[:pid]).nil?)
170
+ Process.kill(self[:timeout_signal],self[:pid])
171
+ sleep(0.1)
172
+ processes.each { |p| Process.kill(self[:timeout_signal],p.pid) unless(Sys::ProcTable.ps(p.pid).nil?) }
173
+ #
174
+ #Process.waitpid(self[:pid]) unless(Sys::ProcTable.ps(self[:pid]).nil?)
130
175
  end
131
176
  def get_child_processes(pid)
132
177
  processes = []
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: execute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.52
4
+ version: 0.1.54
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Marshall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-09 00:00:00.000000000 Z
11
+ date: 2016-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler