@@ -187,47 +187,56 @@ def reload
187
187
def run
188
188
@notify &.status! ( "Initializing..." )
189
189
190
+ with_signal_handlers do
191
+ self . start
192
+
193
+ while @container &.running?
194
+ begin
195
+ @container . wait
196
+ rescue SignalException => exception
197
+ if handler = @signals [ exception . signo ]
198
+ begin
199
+ handler . call
200
+ rescue SetupError => error
201
+ Console . error ( self ) { error }
202
+ end
203
+ else
204
+ raise
205
+ end
206
+ end
207
+ end
208
+ rescue Interrupt
209
+ self . stop
210
+ rescue Terminate
211
+ self . stop ( false )
212
+ ensure
213
+ self . stop ( false )
214
+ end
215
+ end
216
+
217
+ private def with_signal_handlers
190
218
# I thought this was the default... but it doesn't always raise an exception unless you do this explicitly.
191
- # We use `Thread.current.raise(...)` so that exceptions are filtered through `Thread.handle_interrupt` correctly.
219
+
192
220
interrupt_action = Signal . trap ( :INT ) do
193
- # $stderr.puts "Received INT signal, terminating...", caller
221
+ # We use `Thread.current.raise(...)` so that exceptions are filtered through `Thread.handle_interrupt` correctly.
222
+ # $stderr.puts "Received Interrupt signal, terminating...", caller
194
223
::Thread . current . raise ( Interrupt )
195
224
end
196
225
197
226
terminate_action = Signal . trap ( :TERM ) do
198
- # $stderr.puts "Received TERM signal, terminating...", caller
227
+ # $stderr.puts "Received Terminate signal, terminating...", caller
199
228
::Thread . current . raise ( Terminate )
200
229
end
201
230
202
231
hangup_action = Signal . trap ( :HUP ) do
203
- # $stderr.puts "Received HUP signal, restarting...", caller
232
+ # $stderr.puts "Received Hangup signal, restarting...", caller
204
233
::Thread . current . raise ( Hangup )
205
234
end
206
235
207
- self . start
208
-
209
- while @container &.running?
210
- begin
211
- @container . wait
212
- rescue SignalException => exception
213
- if handler = @signals [ exception . signo ]
214
- begin
215
- handler . call
216
- rescue SetupError => error
217
- Console . error ( self ) { error }
218
- end
219
- else
220
- raise
221
- end
222
- end
236
+ ::Thread . handle_interrupt ( SignalException => :on_blocking ) do
237
+ yield
223
238
end
224
- rescue Interrupt
225
- self . stop
226
- rescue Terminate
227
- self . stop ( false )
228
239
ensure
229
- self . stop ( false )
230
-
231
240
# Restore the interrupt handler:
232
241
Signal . trap ( :INT , interrupt_action )
233
242
Signal . trap ( :TERM , terminate_action )
0 commit comments