From 091d9554ad19110844809799e88d6913d9bdcf09 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 27 Dec 2023 19:22:00 +0100 Subject: [PATCH] Print messages on start and shutdown --- Sources/App/Management/SQLiteDatabase.swift | 23 ++++++++++++++++++-- Sources/App/Management/TableManagement.swift | 5 ++++- Sources/App/configure.swift | 6 ++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Sources/App/Management/SQLiteDatabase.swift b/Sources/App/Management/SQLiteDatabase.swift index 495ebe0..b51aa30 100644 --- a/Sources/App/Management/SQLiteDatabase.swift +++ b/Sources/App/Management/SQLiteDatabase.swift @@ -50,6 +50,13 @@ actor SQLiteDatabase { private let mail: MailConfig? private let registeredPlayerCountMetric: Metric + + private let dateFormatter = DateFormatter() + + func printServerStartMessage() async { + let players = await registeredPlayerCountMetric.lastValue()?.value ?? 0 + log("[\(dateFormatter.string(from: Date()))] Server started (\(players) players, \(tables.tableCount) tables)") + } init(database: Database, mail: Configuration.EMail?) async throws { self.registeredPlayerCountMetric = Metric( @@ -60,6 +67,9 @@ actor SQLiteDatabase { self.mail = mail?.mailConfig await updateRegisteredPlayerCount(from: database) + + dateFormatter.dateStyle = .short + dateFormatter.timeStyle = .short } func registerPlayer(named name: PlayerName, hash: PasswordHash, email: String?, in database: Database) async throws -> SessionToken { @@ -86,10 +96,14 @@ actor SQLiteDatabase { await updateRegisteredPlayerCount(from: database) return token } + + private func playerCount(in database: Database) async throws -> Int { + try await User.query(on: database).count() + } func updateRegisteredPlayerCount(from database: Database) async { do { - let count = try await User.query(on: database).count() + let count = try await playerCount(in: database) _ = try? await registeredPlayerCountMetric.update(count) } catch { log("Failed to update player count metric: \(error)") @@ -320,7 +334,12 @@ actor SQLiteDatabase { return try await tables.play(card: card, player: player, in: database) } - func disconnectAllSockets() async { + func shutdown() async { + await disconnectAllSockets() + log("[\(dateFormatter.string(from: Date()))] Server shutdown") + } + + private func disconnectAllSockets() async { await tables.disconnectAllSockets() } } diff --git a/Sources/App/Management/TableManagement.swift b/Sources/App/Management/TableManagement.swift index 6e7ce64..b30560e 100644 --- a/Sources/App/Management/TableManagement.swift +++ b/Sources/App/Management/TableManagement.swift @@ -22,6 +22,10 @@ final class TableManagement { /// The metric describing the number of players currently connected via a websocket private let connectedPlayerCountMetric: Metric + + var tableCount: Int { + tables.count + } /** Load the tables from a file in the storage folder @@ -58,7 +62,6 @@ final class TableManagement { let id = table.id! self.tables[id] = WaitingTable(id: id, name: table.name, isPublic: table.isPublic, players: table.players) } - log("\(tables.count) tables loaded") await logTableCount() await logPlayingPlayerCount() await logConnectedPlayerCount() diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index f962e8b..936b9f2 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -9,7 +9,6 @@ var server: SQLiteDatabase! private var provider: VaporMetricProvider! = nil private var status: Metric! private let scheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2) -private var configurationError: Error? = nil func configure(_ app: Application) async throws { let resourceFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) @@ -73,11 +72,12 @@ func configure(_ app: Application) async throws { monitor.saveCurrentListOfMetricsToLogFolder() try await status.update(.nominal) + await server.printServerStartMessage() } func shutdown() { - scheduler.schedule { - await server.disconnectAllSockets() + Task { + await server.shutdown() try await scheduler.shutdownGracefully() } }