rbbt-util 5.23.0 → 5.23.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rbbt/util/concurrency/processes.rb +74 -52
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1387c90c7d1b469409501f758b509242302e74b1
|
4
|
+
data.tar.gz: 8a8c064c4bb2d8ef569c6d6caa23804cec847f81
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0beeca3f823da88c8cfc94cbc8eefd30692a736eff13ac648a7592985ca4585dc4b7ba72ca2ce990e915b8ee5307e87b920b6bc835bd469807654a0b41b710d5
|
7
|
+
data.tar.gz: 851277c016a171d1db79e3f16d33ef8f967fff05eaba1c33d70d73c7851a3a19153de0ae01902d9d4903ef0e93ecf57c7ddd920ca1a60516aca84e066aa2f21e
|
@@ -3,16 +3,19 @@ require 'rbbt/util/concurrency/processes/socket'
|
|
3
3
|
|
4
4
|
class RbbtProcessQueue
|
5
5
|
|
6
|
-
attr_accessor :num_processes, :
|
6
|
+
attr_accessor :num_processes, :queue, :process_monitor, :cleanup, :join, :reswpan, :offset
|
7
7
|
def initialize(num_processes, cleanup = nil, join = nil, reswpan = nil, offset = false)
|
8
8
|
@num_processes = num_processes
|
9
|
-
@processes = []
|
10
9
|
@cleanup = cleanup
|
11
10
|
@join = join
|
12
11
|
@respawn = reswpan
|
13
12
|
@offset = offset
|
14
13
|
@queue = RbbtProcessSocket.new
|
15
|
-
|
14
|
+
|
15
|
+
key = "/" << rand(1000000000).to_s << '.' << Process.pid.to_s;
|
16
|
+
@sem = key + '.process'
|
17
|
+
Log.debug "Creating process semaphore: #{@sem}"
|
18
|
+
RbbtSemaphore.create_semaphore(@sem,1)
|
16
19
|
end
|
17
20
|
|
18
21
|
|
@@ -66,16 +69,25 @@ class RbbtProcessQueue
|
|
66
69
|
def init(&block)
|
67
70
|
@init_block = block
|
68
71
|
|
72
|
+
RbbtSemaphore.wait_semaphore(@sem)
|
69
73
|
@master_pid = Process.fork do
|
70
74
|
@close_up = false
|
75
|
+
processes_initiated = false
|
76
|
+
process_mutex = Mutex.new
|
71
77
|
|
72
78
|
Signal.trap(CLOSE_SIGNAL) do
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
+
if ! @closing_thread
|
80
|
+
@close_up = true
|
81
|
+
Misc.insist([0,0.01,0.1,0.2,0.5]) do
|
82
|
+
if ! @manager_thread
|
83
|
+
Thread.pass
|
84
|
+
raise "Manager thread for #{Process.pid} not found yet"
|
85
|
+
end
|
86
|
+
|
87
|
+
if @manager_thread.alive?
|
88
|
+
raise "Manager thread for #{Process.pid} not working yet" unless @manager_thread["working"]
|
89
|
+
@manager_thread.raise TryAgain
|
90
|
+
end
|
79
91
|
end
|
80
92
|
end
|
81
93
|
end
|
@@ -91,23 +103,20 @@ class RbbtProcessQueue
|
|
91
103
|
end
|
92
104
|
|
93
105
|
Signal.trap(ABORT_SIGNAL) do
|
94
|
-
|
95
|
-
@monitor_thread.raise Aborted
|
96
|
-
rescue Exception
|
97
|
-
Log.exception $!
|
106
|
+
Misc.insist([0,0.01,0.1,0.2,0.5]) do
|
107
|
+
@monitor_thread.raise Aborted if @monitor_thread && @monitor_thread.alive?
|
98
108
|
end
|
99
109
|
end
|
100
110
|
|
101
|
-
|
102
111
|
if @callback_queue
|
103
|
-
Misc.purge_pipes(@queue.swrite
|
112
|
+
Misc.purge_pipes(@queue.swrite, @queue.sread, @callback_queue.swrite, @callback_queue.sread)
|
104
113
|
else
|
105
|
-
Misc.purge_pipes(@queue.swrite
|
114
|
+
Misc.purge_pipes(@queue.swrite, @queue.sread)
|
106
115
|
end
|
107
116
|
|
108
|
-
@total =
|
117
|
+
@total = 0
|
109
118
|
@count = 0
|
110
|
-
|
119
|
+
processes = []
|
111
120
|
|
112
121
|
@manager_thread = Thread.new do
|
113
122
|
begin
|
@@ -115,15 +124,19 @@ class RbbtProcessQueue
|
|
115
124
|
begin
|
116
125
|
Thread.current["working"] = true
|
117
126
|
if @close_up
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
+
Thread.handle_interrupt(TryAgain => :never) do
|
128
|
+
Log.debug "Closing up process queue #{Process.pid}"
|
129
|
+
@count = 0
|
130
|
+
@closing_thread = Thread.new do
|
131
|
+
Thread.handle_interrupt(TryAgain => :never) do
|
132
|
+
Log.debug "Pushing closed stream #{Process.pid}"
|
133
|
+
while true
|
134
|
+
@queue.push ClosedStream.new unless @queue.cleaned
|
135
|
+
break if processes_initiated && processes.empty?
|
136
|
+
end unless processes_initiated && processes.empty?
|
137
|
+
end
|
138
|
+
end
|
139
|
+
@close_up = false
|
127
140
|
end
|
128
141
|
end
|
129
142
|
|
@@ -134,20 +147,22 @@ class RbbtProcessQueue
|
|
134
147
|
|
135
148
|
raise TryAgain if @close_up
|
136
149
|
|
137
|
-
|
138
|
-
|
139
|
-
@count
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
@count
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
150
|
+
process_mutex.synchronize do
|
151
|
+
Thread.handle_interrupt(TryAgain => :never) do
|
152
|
+
while @count > 0
|
153
|
+
@count -= 1
|
154
|
+
@total += 1
|
155
|
+
processes << RbbtProcessQueueWorker.new(@queue, @callback_queue, @cleanup, @respawn, @offset, &@init_block)
|
156
|
+
Log.warn "Added process #{processes.last.pid} to #{Process.pid} (#{processes.length})"
|
157
|
+
end
|
158
|
+
|
159
|
+
while @count < 0
|
160
|
+
@count += 1
|
161
|
+
next unless processes.length > 1
|
162
|
+
first = processes.shift
|
163
|
+
first.stop
|
164
|
+
Log.warn "Removed process #{first.pid} from #{Process.pid} (#{processes.length})"
|
165
|
+
end
|
151
166
|
end
|
152
167
|
end
|
153
168
|
rescue TryAgain
|
@@ -169,34 +184,38 @@ class RbbtProcessQueue
|
|
169
184
|
@callback_queue.close_read if @callback_queue
|
170
185
|
|
171
186
|
num_processes.times do |i|
|
172
|
-
@
|
173
|
-
|
187
|
+
@total += 1
|
188
|
+
process_mutex.synchronize do
|
189
|
+
processes << RbbtProcessQueueWorker.new(@queue, @callback_queue, @cleanup, @respawn, @offset, &@init_block)
|
174
190
|
end
|
175
191
|
end
|
176
192
|
|
193
|
+
processes_initiated = true
|
194
|
+
|
177
195
|
@monitor_thread = Thread.new do
|
178
196
|
begin
|
179
|
-
while
|
180
|
-
|
181
|
-
|
197
|
+
while processes.any?
|
198
|
+
processes[0].join
|
199
|
+
processes.shift
|
182
200
|
end
|
183
201
|
rescue Aborted
|
184
202
|
Log.warn "Aborting process monitor"
|
185
|
-
|
186
|
-
|
203
|
+
processes.each{|p| p.abort_and_join}
|
204
|
+
processes.clear
|
187
205
|
|
188
206
|
@callback_thread.kill if @callback_thread && @callback_thread.alive?
|
189
207
|
@manager_thread.kill if @manager_thread.alive?
|
190
208
|
rescue Exception
|
191
209
|
Log.warn "Process monitor exception: #{$!.message}"
|
192
|
-
|
193
|
-
|
210
|
+
processes.each{|p| p.abort_and_join}
|
211
|
+
processes.clear
|
194
212
|
|
195
213
|
@callback_thread.kill if @callback_thread && @callback_thread.alive?
|
196
214
|
@manager_thread.kill if @manager_thread.alive?
|
197
215
|
end
|
198
216
|
end
|
199
217
|
|
218
|
+
RbbtSemaphore.post_semaphore(@sem)
|
200
219
|
|
201
220
|
begin
|
202
221
|
|
@@ -233,7 +252,7 @@ class RbbtProcessQueue
|
|
233
252
|
Log.warn "Error closing callback: #{$!.message}"
|
234
253
|
end
|
235
254
|
@callback_thread.join if @callback_thread && @callback_thread.alive?
|
236
|
-
t.join
|
255
|
+
t.join if t && t.alive?
|
237
256
|
end
|
238
257
|
|
239
258
|
def _join
|
@@ -265,7 +284,9 @@ class RbbtProcessQueue
|
|
265
284
|
|
266
285
|
def join
|
267
286
|
begin
|
268
|
-
|
287
|
+
RbbtSemaphore.synchronize(@sem) do
|
288
|
+
Process.kill CLOSE_SIGNAL, @master_pid
|
289
|
+
end
|
269
290
|
rescue Errno::ECHILD, Errno::ESRCH
|
270
291
|
Log.debug "Cannot kill #{@master_pid}: #{$!.message}"
|
271
292
|
end
|
@@ -296,6 +317,7 @@ class RbbtProcessQueue
|
|
296
317
|
end
|
297
318
|
|
298
319
|
def clean
|
320
|
+
RbbtSemaphore.delete_semaphore(@sem)
|
299
321
|
begin
|
300
322
|
self.abort if Misc.pid_exists?(@master_pid)
|
301
323
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbbt-util
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.23.
|
4
|
+
version: 5.23.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Miguel Vazquez
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|