1010require_relative "notify"
1111require_relative "policy"
1212require_relative "events"
13- require_relative "signals"
13+
14+ require "async/signals"
1415
1516module Async
1617 module Container
@@ -29,6 +30,25 @@ module Container
2930 # Manages the life-cycle of one or more containers in order to support a persistent system.
3031 # e.g. a web server, job server or some other long running system.
3132 class Controller
33+ # Represents a trapped process signal as a queued controller event.
34+ class SignalEvent
35+ # Initialize the signal event.
36+ # @parameter signal [Symbol | String | Integer] The signal that was received.
37+ # @parameter handler [Proc] The handler to invoke when the event is processed.
38+ def initialize ( signal , handler )
39+ @signal = signal
40+ @handler = handler
41+ end
42+
43+ # @attribute [Symbol | String | Integer] The signal that was received.
44+ attr :signal
45+
46+ # Process the signal event by invoking the registered handler.
47+ def call
48+ @handler . call
49+ end
50+ end
51+
3252 SIGHUP = Signal . list [ "HUP" ]
3353 SIGINT = Signal . list [ "INT" ]
3454 SIGTERM = Signal . list [ "TERM" ]
@@ -44,7 +64,7 @@ def initialize(notify: Notify.open!, container_class: Container, graceful_stop:
4464
4565 @container = nil
4666 @events = Events . new
47- @signals = Signals . new ( @events )
67+ @signals = Async :: Signals :: Handlers . new
4868
4969 self . trap ( SIGHUP ) do
5070 self . restart
@@ -101,7 +121,15 @@ def to_s
101121 # @parameters signal [Symbol] The signal to trap, e.g. `:INT`.
102122 # @parameters block [Proc] The signal handler to invoke.
103123 def trap ( signal , &block )
104- @signals . trap ( signal , &block )
124+ if block
125+ event = SignalEvent . new ( signal , block ) . freeze
126+
127+ @signals . trap ( signal ) do
128+ @events << event
129+ end
130+ else
131+ @signals . ignore ( signal )
132+ end
105133 end
106134
107135 # Create a policy for managing child lifecycle events.
@@ -247,7 +275,7 @@ def reload
247275 def run
248276 @notify &.status! ( "Initializing controller..." )
249277
250- @signals . trapped do
278+ Async :: Signals . install ( @signals ) do
251279 self . start
252280
253281 while event = @events . pop ( timeout : 0 )
0 commit comments