-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose
graceful_stop
for controlling graceful shutdown. (#31)
- Loading branch information
Showing
8 changed files
with
180 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2024, by Samuel Williams. | ||
|
||
require '../../lib/async/container' | ||
require 'async/io/host_endpoint' | ||
|
||
Console.logger.debug! | ||
|
||
module SignalWrapper | ||
def self.trap(signal, &block) | ||
signal = signal | ||
|
||
original = Signal.trap(signal) do | ||
::Signal.trap(signal, original) | ||
block.call | ||
end | ||
end | ||
end | ||
|
||
class Controller < Async::Container::Controller | ||
def initialize(...) | ||
super | ||
|
||
@endpoint = Async::IO::Endpoint.tcp("localhost", 8080) | ||
@bound_endpoint = nil | ||
end | ||
|
||
def start | ||
Console.debug(self) {"Binding to #{@endpoint}"} | ||
@bound_endpoint = Sync{@endpoint.bound} | ||
|
||
super | ||
end | ||
|
||
def setup(container) | ||
container.run count: 2, restart: true do |instance| | ||
SignalWrapper.trap(:INT) do | ||
Console.debug(self) {"Closing bound instance..."} | ||
@bound_endpoint.close | ||
end | ||
|
||
Sync do |task| | ||
Console.info(self) {"Starting bound instance..."} | ||
|
||
instance.ready! | ||
|
||
@bound_endpoint.accept do |peer| | ||
while true | ||
peer.write("#{Time.now.to_s.rjust(32)}: Hello World\n") | ||
sleep 1 | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def stop(graceful = true) | ||
super | ||
|
||
if @bound_endpoint | ||
@bound_endpoint.close | ||
@bound_endpoint = nil | ||
end | ||
end | ||
end | ||
|
||
controller = Controller.new | ||
|
||
controller.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2020-2022, by Samuel Williams. | ||
|
||
require_relative '../../../lib/async/container/controller' | ||
|
||
$stdout.sync = true | ||
|
||
class Graceful < Async::Container::Controller | ||
def setup(container) | ||
container.run(name: "graceful", count: 1, restart: true) do |instance| | ||
instance.ready! | ||
|
||
# This is to avoid race conditions in the controller in test conditions. | ||
sleep 0.1 | ||
|
||
clock = Async::Clock.start | ||
|
||
original_action = Signal.trap(:INT) do | ||
# We ignore the int, but in practical applications you would want start a graceful shutdown. | ||
$stdout.puts "Graceful shutdown...", clock.total | ||
|
||
Signal.trap(:INT, original_action) | ||
end | ||
|
||
$stdout.puts "Ready...", clock.total | ||
|
||
sleep | ||
ensure | ||
$stdout.puts "Exiting...", clock.total | ||
end | ||
end | ||
end | ||
|
||
controller = Graceful.new(graceful_stop: 1) | ||
|
||
begin | ||
controller.run | ||
rescue Async::Container::Terminate | ||
$stdout.puts "Terminated..." | ||
rescue Interrupt | ||
$stdout.puts "Interrupted..." | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters