Close all sockets on shutdown

This commit is contained in:
Christoph Hagen 2022-01-24 17:15:11 +01:00
parent 707c6d03de
commit 89007989e3
5 changed files with 21 additions and 0 deletions

View File

@ -174,4 +174,8 @@ final class SQLiteDatabase {
}
return tables.play(card: card, player: player)
}
func disconnectAllSockets() {
tables.disconnectAllSockets()
}
}

View File

@ -199,4 +199,8 @@ final class TableManagement {
// TODO: Save new table
return .success
}
func disconnectAllSockets() {
tables.values.forEach { $0.disconnectAllPlayers() }
}
}

View File

@ -124,6 +124,10 @@ extension AbstractTable: ManageableTable {
return
}
func disconnectAllPlayers() {
players.forEach { $0.disconnect() }
}
func sendUpdateToAllPlayers() {
players.enumerated().forEach { playerIndex, player in
guard player.isConnected else {

View File

@ -31,4 +31,6 @@ protocol ManageableTable {
func disconnect(player name: PlayerName)
func sendUpdateToAllPlayers()
func disconnectAllPlayers()
}

View File

@ -33,6 +33,13 @@ public func configure(_ app: Application) throws {
let db = app.databases.database(.sqlite, logger: .init(label: "Init"), on: app.databases.eventLoopGroup.next())!
server = try SQLiteDatabase(db: db)
// Gracefully shut down by closing potentially open socket
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) {
_ = app.server.onShutdown.always { _ in
server.disconnectAllSockets()
}
}
// register routes
try routes(app)
}