execute 0.1.60 → 0.1.62

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 +23 -29
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ef06380e0ee0edf326fa8418bc8c2c8b49b646b
4
- data.tar.gz: 5e19c0139037ea6b21b88f66b66bc8cf6210913c
3
+ metadata.gz: c03b6064b5618dfe687c78adc1aafa012c1442ca
4
+ data.tar.gz: a6af42c420a9a50f1c35e9e3fbe576aa9cefba23
5
5
  SHA512:
6
- metadata.gz: 235a92e95e683b3c007fe903f7cb3a4bca903897ae321a4e5236a33b7366ead853904e4a89b029f116f13b4080a7b05295827bed4aa8e7135c318767a622e7fb
7
- data.tar.gz: 9d43f1145228df94a3ebd8a5197211bf563be3fe96afb533887ae6a37f6f1e580d093d5b260ee689b42b1e3555aeebc0ccb0c15ef8df9570a5e3297e0492ef65
6
+ metadata.gz: b2dbd1f768929fb92cdd344bc8d57c733e79a2ad964b30c450116a3458e3f1e6c1356894561dca5cb1fdf988199767d8d34ea67ec1a43ca6e49b81b195add165
7
+ data.tar.gz: 306ae3dda19c7ca8bcfc984963918385c443d7808f27b7687428258cbe0b33b234c831a4f406cf7efc2d760919fca4db43b053fe5f229566f54a37afc7445f1f
data/lib/cmd.rb CHANGED
@@ -49,8 +49,8 @@ class CMD < Hash
49
49
  end
50
50
 
51
51
  puts self[:command] if(self[:echo_command] || self[:debug])
52
- call_popen
53
- #call_capture
52
+ self[:elapsed_time] = Benchmark.realtime { call_popen }
53
+ #self[:elapsed_time] = Benchmark.realtime { call_capture }
54
54
 
55
55
  if(self[:debug])
56
56
  puts "command: #{self[:command]}"
@@ -73,7 +73,6 @@ class CMD < Hash
73
73
  begin
74
74
  output = ''
75
75
  error = ''
76
- Thread.abort_on_exception = true
77
76
  mutex = Mutex.new
78
77
 
79
78
  Open3.popen3(self[:command]) do |stdin, stdout, stderr, wait_thr|
@@ -83,11 +82,10 @@ class CMD < Hash
83
82
  start_time = Time.now
84
83
  Thread.new do
85
84
  while wait_thr.alive? do
86
- sleep(0.1)
87
85
  if((Time.now - start_time).to_f > self[:timeout])
88
86
  self[:timed_out] = true
89
87
  interrupt
90
- sleep(0.1)
88
+ Thread.stop
91
89
  end
92
90
  end
93
91
  end
@@ -95,18 +93,22 @@ class CMD < Hash
95
93
 
96
94
  {:output => stdout,:error => stderr}.each do |key, stream|
97
95
  Thread.new do
98
- while wait_thr.alive? && !key?(:timed_out) do
99
- if(!stream.closed? && !(char = stream.getc).nil?)
100
- case key
101
- when :output
102
- output << char
103
- putc char if(self[:echo_output])
104
- when :error
105
- error << char
96
+ begin
97
+ while wait_thr.alive? && !key?(:timed_out) && !@stop_threads do
98
+ unless(stream.closed?)
99
+ unless((char = stream.getc).nil?)
100
+ case key
101
+ when :output
102
+ output << char
103
+ putc char if(self[:echo_output])
104
+ when :error
105
+ error << char
106
+ end
107
+ end
106
108
  end
107
- else
108
- sleep(0.1)
109
109
  end
110
+ rescue IOError
111
+ Thread.stop
110
112
  end
111
113
  end
112
114
  end
@@ -117,16 +119,12 @@ class CMD < Hash
117
119
  self[:error] = error unless(error.empty?)
118
120
  self[:exit_code] = wait_thr.value.to_i
119
121
  end
120
- rescue Interrupt => e
121
- interrupt
122
- raise e
123
122
  rescue Exception => e
124
123
  self[:error] = "#{self[:error]}\nException: #{e.to_s}"
125
124
  self[:exit_code]=1 unless(self[:exit_code].nil? || (self[:exit_code] == 0))
126
125
  end
127
126
  end
128
127
  def call_capture
129
- elapsed_time = nil
130
128
  begin
131
129
  if(key?(:timeout))
132
130
  start_time = Time.now
@@ -144,10 +142,8 @@ class CMD < Hash
144
142
  end
145
143
 
146
144
  self[:output] = self[:error] = ''
147
- elapsed_time = Benchmark.realtime do
148
- self[:output], self[:error], status = Open3.capture3(self[:command])
149
- self[:exit_code] = status.to_i
150
- end
145
+ self[:output], self[:error], status = Open3.capture3(self[:command])
146
+ self[:exit_code] = status.to_i
151
147
 
152
148
  puts self[:output] if(self[:echo_output] && !self[:output].empty?)
153
149
 
@@ -155,8 +151,6 @@ class CMD < Hash
155
151
  rescue Exception => e
156
152
  self[:error] = "#{self[:error]}\nException: #{e.to_s}"
157
153
  self[:exit_code]=1 unless(!self[:exit_code].nil? || (self[:exit_code] == 0))
158
- ensure
159
- self[:elapsed_time] = elapsed_time unless(elapsed_time.nil?)
160
154
  end
161
155
  end
162
156
  def command_pid
@@ -173,12 +167,12 @@ class CMD < Hash
173
167
  raise "Do not have a process id for #{self[:pid]}" unless(key?(:pid))
174
168
  processes = get_child_processes(self[:pid])
175
169
 
176
- Process.kill(self[:timeout_signal],self[:pid])
177
- sleep(0.1)
170
+ Process.kill(self[:timeout_signal], self[:pid])
171
+ Process.waitpid(self[:pid]) unless(Sys::ProcTable.ps(self[:pid]).nil?)
172
+
178
173
  processes.each { |p| Process.kill(self[:timeout_signal],p.pid) unless(Sys::ProcTable.ps(p.pid).nil?) }
179
- #
180
- #Process.waitpid(self[:pid]) unless(Sys::ProcTable.ps(self[:pid]).nil?)
181
174
  end
175
+
182
176
  def get_child_processes(pid)
183
177
  processes = []
184
178
  Sys::ProcTable.ps do |p|
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.60
4
+ version: 0.1.62
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-12 00:00:00.000000000 Z
11
+ date: 2016-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler