@@ -20,10 +20,12 @@ class Supervisor
20
20
WorkerContext = Struct . new ( :worker , :type , :restart ) do
21
21
attr_accessor :thread
22
22
attr_accessor :terminated
23
+
23
24
def alive? ( ) return thread && thread . alive? ; end
25
+
24
26
def needs_restart?
25
27
return false if thread && thread . alive?
26
- return false if terminated == true
28
+ return false if terminated
27
29
case self . restart
28
30
when :permanent
29
31
return true
@@ -42,12 +44,12 @@ def add(context)
42
44
self . supervisors += 1 if context . type == :supervisor
43
45
self . workers += 1 if context . type == :worker
44
46
end
45
- def active ( ) sleeping + running + aborting end ;
46
- def sleeping ( ) @status . reduce ( 0 ) { |x , s | x += ( s == 'sleep' ? 1 : 0 ) } end ;
47
- def running ( ) @status . reduce ( 0 ) { |x , s | x += ( s == 'run' ? 1 : 0 ) } end ;
48
- def aborting ( ) @status . reduce ( 0 ) { |x , s | x += ( s == 'aborting' ? 1 : 0 ) } end ;
49
- def stopped ( ) @status . reduce ( 0 ) { |x , s | x += ( s == false ? 1 : 0 ) } end ;
50
- def abend ( ) @status . reduce ( 0 ) { |x , s | x += ( s . nil? ? 1 : 0 ) } end ;
47
+ def active ( ) sleeping + running + aborting end
48
+ def sleeping ( ) @status . reduce ( 0 ) { |x , s | x += ( s == 'sleep' ? 1 : 0 ) } end
49
+ def running ( ) @status . reduce ( 0 ) { |x , s | x += ( s == 'run' ? 1 : 0 ) } end
50
+ def aborting ( ) @status . reduce ( 0 ) { |x , s | x += ( s == 'aborting' ? 1 : 0 ) } end
51
+ def stopped ( ) @status . reduce ( 0 ) { |x , s | x += ( s == false ? 1 : 0 ) } end
52
+ def abend ( ) @status . reduce ( 0 ) { |x , s | x += ( s . nil? ? 1 : 0 ) } end
51
53
end
52
54
53
55
attr_reader :monitor_interval
@@ -100,12 +102,13 @@ def run
100
102
@running = true
101
103
end
102
104
monitor
103
- return true
105
+ true
104
106
end
105
107
106
108
def stop
107
- return true unless @running
108
109
@mutex . synchronize do
110
+ return true unless @running
111
+
109
112
@running = false
110
113
unless @monitor . nil?
111
114
@monitor . run if @monitor . status == 'sleep'
@@ -122,54 +125,55 @@ def stop
122
125
end
123
126
prune_workers
124
127
end
125
- return true
128
+
129
+ true
126
130
end
127
131
128
132
def running?
129
- return @running == true
133
+ @mutex . synchronize { @running }
130
134
end
131
135
132
136
def length
133
- return @workers . length
137
+ @mutex . synchronize { @workers . length }
134
138
end
135
139
alias_method :size , :length
136
140
137
141
def current_restart_count
138
- return @restart_times . length
142
+ @restart_times . length
139
143
end
140
144
141
145
def count
142
- return @mutex . synchronize do
146
+ @mutex . synchronize do
143
147
@count . status = @workers . collect { |w | w . thread ? w . thread . status : false }
144
148
@count . dup . freeze
145
149
end
146
150
end
147
151
148
152
def add_worker ( worker , opts = { } )
149
153
return nil if worker . nil? || ! behaves_as_worker? ( worker )
150
- return @mutex . synchronize {
154
+ @mutex . synchronize {
151
155
restart = opts [ :restart ] || :permanent
152
156
type = opts [ :type ] || ( worker . is_a? ( Supervisor ) ? :supervisor : nil ) || :worker
153
157
raise ArgumentError . new ( ":#{ restart } is not a valid restart option" ) unless CHILD_RESTART_OPTIONS . include? ( restart )
154
158
raise ArgumentError . new ( ":#{ type } is not a valid child type" ) unless CHILD_TYPES . include? ( type )
155
159
context = WorkerContext . new ( worker , type , restart )
156
160
@workers << context
157
161
@count . add ( context )
158
- worker . run if running?
162
+ worker . run if @ running
159
163
context . object_id
160
164
}
161
165
end
162
166
alias_method :add_child , :add_worker
163
167
164
168
def add_workers ( workers , opts = { } )
165
- return workers . collect do |worker |
169
+ workers . collect do |worker |
166
170
add_worker ( worker , opts )
167
171
end
168
172
end
169
173
alias_method :add_children , :add_workers
170
174
171
175
def remove_worker ( worker_id )
172
- return @mutex . synchronize do
176
+ @mutex . synchronize do
173
177
index , context = find_worker ( worker_id )
174
178
break ( nil ) if context . nil?
175
179
break ( false ) if context . alive?
@@ -180,8 +184,9 @@ def remove_worker(worker_id)
180
184
alias_method :remove_child , :remove_worker
181
185
182
186
def stop_worker ( worker_id )
183
- return true unless running?
184
- return @mutex . synchronize do
187
+ @mutex . synchronize do
188
+ return true unless @running
189
+
185
190
index , context = find_worker ( worker_id )
186
191
break ( nil ) if index . nil?
187
192
context . terminated = true
@@ -193,8 +198,9 @@ def stop_worker(worker_id)
193
198
alias_method :stop_child , :stop_worker
194
199
195
200
def start_worker ( worker_id )
196
- return false unless running?
197
- return @mutex . synchronize do
201
+ @mutex . synchronize do
202
+ return false unless @running
203
+
198
204
index , context = find_worker ( worker_id )
199
205
break ( nil ) if context . nil?
200
206
context . terminated = false
@@ -205,8 +211,9 @@ def start_worker(worker_id)
205
211
alias_method :start_child , :start_worker
206
212
207
213
def restart_worker ( worker_id )
208
- return false unless running?
209
- return @mutex . synchronize do
214
+ @mutex . synchronize do
215
+ return false unless @running
216
+
210
217
index , context = find_worker ( worker_id )
211
218
break ( nil ) if context . nil?
212
219
break ( false ) if context . restart == :temporary
@@ -221,7 +228,7 @@ def restart_worker(worker_id)
221
228
private
222
229
223
230
def behaves_as_worker? ( obj )
224
- return WORKER_API . each do |method , arity |
231
+ WORKER_API . each do |method , arity |
225
232
break ( false ) unless obj . respond_to? ( method ) && obj . method ( method ) . arity == arity
226
233
true
227
234
end
@@ -247,7 +254,7 @@ def run_worker(context)
247
254
Thread . current . abort_on_exception = false
248
255
context . worker . run
249
256
end
250
- return context
257
+ context
251
258
end
252
259
253
260
def terminate_worker ( context )
@@ -272,9 +279,9 @@ def prune_workers
272
279
def find_worker ( worker_id )
273
280
index = @workers . find_index { |worker | worker . object_id == worker_id }
274
281
if index . nil?
275
- return [ nil , nil ]
282
+ [ nil , nil ]
276
283
else
277
- return [ index , @workers [ index ] ]
284
+ [ index , @workers [ index ] ]
278
285
end
279
286
end
280
287
@@ -286,7 +293,7 @@ def exceeded_max_restart_frequency?
286
293
elsif diff >= @max_time
287
294
@restart_times . pop
288
295
end
289
- return false
296
+ false
290
297
end
291
298
292
299
#----------------------------------------------------------------
0 commit comments