execute 0.1.72 → 0.1.73
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/execute.rb +58 -56
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55471a7bdfc605188b00b9398701e5eb7930ba5a
|
4
|
+
data.tar.gz: 65572161753f59536c950cb281ca4eadf5e7d7ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5dea0908aca1cf83e27a7442cf869ab40e2ed906eec4a7a55c291e742e3c7b68d0e687a46a0698e45099b590d0499176f75c5f0c07356bf482769c57628a3e6
|
7
|
+
data.tar.gz: 2158ac84626394f6a3d7d94bab3dc39335243a499a9bde908fcea1bc4628cfd6a3a486fec2ba2498057fb28d7c7758285019ab01c3dc7a4e628b8e32fda16ef1
|
data/lib/execute.rb
CHANGED
@@ -3,6 +3,9 @@ require 'sys/proctable'
|
|
3
3
|
require 'timeout'
|
4
4
|
require 'benchmark'
|
5
5
|
|
6
|
+
class TimeoutException < Exception
|
7
|
+
end
|
8
|
+
|
6
9
|
class Execute < Hash
|
7
10
|
private
|
8
11
|
@@default_options = { echo_command: true, echo_output: true, ignore_exit_code: false, debug: false }
|
@@ -59,7 +62,7 @@ class Execute < Hash
|
|
59
62
|
puts "exit_code: #{self[:exit_code]}"
|
60
63
|
end
|
61
64
|
|
62
|
-
raise
|
65
|
+
raise TimeoutException.new("Command '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error])
|
63
66
|
|
64
67
|
if((self[:exit_code] != 0) && !self[:ignore_exit_code])
|
65
68
|
exception_text = "Command: '#{self[:command]}'"
|
@@ -70,9 +73,9 @@ class Execute < Hash
|
|
70
73
|
end
|
71
74
|
end
|
72
75
|
|
73
|
-
|
76
|
+
def call_popen
|
74
77
|
begin
|
75
|
-
|
78
|
+
output = ''
|
76
79
|
error = ''
|
77
80
|
|
78
81
|
threads = []
|
@@ -82,9 +85,9 @@ class Execute < Hash
|
|
82
85
|
timeout = self[:timeout] if(key?(:timeout))
|
83
86
|
|
84
87
|
Open3.popen3(self[:command]) do |stdin, stdout, stderr, wait_thr|
|
85
|
-
|
88
|
+
self[:pid] = wait_thr.pid
|
86
89
|
|
87
|
-
|
90
|
+
unless(timeout.nil?)
|
88
91
|
start_time = Time.now
|
89
92
|
threads << Thread.new do
|
90
93
|
begin
|
@@ -104,85 +107,84 @@ class Execute < Hash
|
|
104
107
|
mutex.synchronize { stop_threads = true }
|
105
108
|
end
|
106
109
|
end
|
107
|
-
|
110
|
+
end
|
108
111
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
112
|
+
{:output => stdout,:error => stderr}.each do |key, stream|
|
113
|
+
threads << Thread.new do
|
114
|
+
begin
|
115
|
+
last_pass_time = Time.now
|
113
116
|
while wait_thr.alive? do
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
117
|
+
while !stream.closed? &&
|
118
|
+
!(char = stream.getc).nil? do
|
119
|
+
case key
|
120
|
+
when :output
|
121
|
+
output << char
|
122
|
+
putc char if(self[:echo_output])
|
123
|
+
when :error
|
124
|
+
error << char
|
125
|
+
end
|
123
126
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
127
|
+
if(wait_thr.alive? && ((Time.now - last_pass_time).to_i > 15))
|
128
|
+
last_pass_time = Time.now
|
129
|
+
Thread.pass
|
130
|
+
end
|
131
|
+
end
|
132
|
+
break if(stop_threads)
|
133
|
+
sleep(0.1)
|
128
134
|
end
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
rescue Exception
|
134
|
-
mutex.synchronize { stop_threads = true }
|
135
|
-
end
|
135
|
+
mutex.synchronize { stop_threads = true }
|
136
|
+
rescue Exception
|
137
|
+
mutex.synchronize { stop_threads = true }
|
138
|
+
end
|
136
139
|
end
|
137
140
|
end
|
138
141
|
|
139
|
-
threads.each { |thr| thr.join }
|
140
|
-
|
142
|
+
threads.each { |thr| thr.join }
|
141
143
|
self[:output] = output unless(output.empty?)
|
142
|
-
|
143
|
-
|
144
|
+
self[:error] = error unless(error.empty?)
|
145
|
+
self[:exit_code] = wait_thr.value.to_i
|
144
146
|
end
|
145
147
|
rescue Exception => e
|
146
148
|
self[:error] = "#{self[:error]}\nException: #{e.to_s}"
|
147
|
-
|
149
|
+
self[:exit_code]=1 unless(self[:exit_code].nil? || (self[:exit_code] == 0))
|
148
150
|
end
|
149
|
-
|
150
|
-
|
151
|
+
end
|
152
|
+
def call_capture
|
151
153
|
begin
|
152
|
-
|
154
|
+
if(key?(:timeout))
|
153
155
|
start_time = Time.now
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
156
|
+
Thread.new do
|
157
|
+
while !key?(:exit_code) do
|
158
|
+
sleep(0.1)
|
159
|
+
if((Time.now - start_time).to_f > self[:timeout])
|
160
|
+
self[:timed_out] = true
|
161
|
+
interrupt
|
162
|
+
sleep(0.1)
|
163
|
+
break
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
165
167
|
end
|
166
168
|
|
167
|
-
|
169
|
+
self[:output] = self[:error] = ''
|
168
170
|
self[:output], self[:error], status = Open3.capture3(self[:command])
|
169
171
|
self[:exit_code] = status.to_i
|
170
172
|
|
171
173
|
puts self[:output] if(self[:echo_output] && !self[:output].empty?)
|
172
174
|
|
173
|
-
raise
|
175
|
+
raise TimeoutException.new("Command '#{self[:command]}' timed out after #{self[:timeout]} seconds") if(key?(:timed_out) && self[:timeout_raise_error])
|
174
176
|
rescue Exception => e
|
175
177
|
self[:error] = "#{self[:error]}\nException: #{e.to_s}"
|
176
178
|
self[:exit_code]=1 unless(!self[:exit_code].nil? || (self[:exit_code] == 0))
|
177
179
|
end
|
178
|
-
|
180
|
+
end
|
179
181
|
|
180
|
-
|
181
|
-
|
182
|
+
def []=(key,value)
|
183
|
+
mutex = Mutex.new
|
182
184
|
mutex.synchronize { super(key, value) }
|
183
|
-
|
185
|
+
end
|
184
186
|
|
185
|
-
|
187
|
+
def [](key)
|
186
188
|
value = nil
|
187
189
|
mutex = Mutex.new
|
188
190
|
mutex.synchronize { value = super(key) }
|
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.
|
4
|
+
version: 0.1.73
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Marshall
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
110
|
version: '0'
|
111
111
|
requirements: []
|
112
112
|
rubyforge_project:
|
113
|
-
rubygems_version: 2.
|
113
|
+
rubygems_version: 2.5.2
|
114
114
|
signing_key:
|
115
115
|
specification_version: 4
|
116
116
|
summary: Class wrapper for system commands
|