Update dependencies

This commit is contained in:
Christoph Hagen 2023-10-02 00:37:50 +02:00
parent 9db072d769
commit aa474c365f
5 changed files with 59 additions and 14 deletions

View File

@ -15,8 +15,8 @@ let package = Package(
// https://github.com/Joannis/SMTPKitten <- No updates since 0ct 2020 // https://github.com/Joannis/SMTPKitten <- No updates since 0ct 2020
// https://github.com/Joannis/VaporSMTPKit <- No updates since 0ct. 2020, uses SMTPKitten // https://github.com/Joannis/VaporSMTPKit <- No updates since 0ct. 2020, uses SMTPKitten
.package(url: "https://github.com/Kitura/Swift-SMTP", from: "6.0.0"), .package(url: "https://github.com/Kitura/Swift-SMTP", from: "6.0.0"),
.package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.9.0"), .package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.11.2"),
.package(url: "https://github.com/christophhagen/ClairvoyantVapor", from: "0.2.0"), .package(url: "https://github.com/christophhagen/ClairvoyantVapor", from: "0.5.0"),
.package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"), .package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"),
], ],
targets: [ targets: [

View File

@ -0,0 +1,34 @@
import Foundation
import Clairvoyant
import Vapor
import NIOCore
final class EventLoopScheduler {
private let backgroundGroup: EventLoopGroup
init(numberOfThreads: Int = 2) {
backgroundGroup = MultiThreadedEventLoopGroup(numberOfThreads: numberOfThreads)
}
func next() -> EventLoop {
backgroundGroup.next()
}
func provider() -> NIOEventLoopGroupProvider {
return .shared(backgroundGroup)
}
func shutdown() {
backgroundGroup.shutdownGracefully { _ in
}
}
}
extension EventLoopScheduler: AsyncScheduler {
func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
_ = backgroundGroup.any().makeFutureWithTask(asyncJob)
}
}

View File

@ -52,7 +52,7 @@ final class SQLiteDatabase {
private let registeredPlayerCountMetric: Metric<Int> private let registeredPlayerCountMetric: Metric<Int>
init(database: Database, mail: Configuration.EMail?) async throws { init(database: Database, mail: Configuration.EMail?) async throws {
self.registeredPlayerCountMetric = try await Metric( self.registeredPlayerCountMetric = Metric(
"schafkopf.players", "schafkopf.players",
name: "Number of users", name: "Number of users",
description: "The total number of user accounts") description: "The total number of user accounts")

View File

@ -28,15 +28,15 @@ final class TableManagement {
- Throws: Errors when the file could not be read - Throws: Errors when the file could not be read
*/ */
init(database: Database) async throws { init(database: Database) async throws {
self.tableCountMetric = try await .init( self.tableCountMetric = .init(
"schafkopf.tables", "schafkopf.tables",
name: "Open tables", name: "Open tables",
description: "The number of currently available tables") description: "The number of currently available tables")
self.playingPlayerCountMetric = try await .init( self.playingPlayerCountMetric = .init(
"schafkopf.playing", "schafkopf.playing",
name: "Sitting players", name: "Sitting players",
description: "The number of players currently sitting at a table") description: "The number of players currently sitting at a table")
self.connectedPlayerCountMetric = try await .init( self.connectedPlayerCountMetric = .init(
"schafkopf.connected", "schafkopf.connected",
name: "Connected players", name: "Connected players",
description: "The number of players with a websocket connection to the server") description: "The number of players with a websocket connection to the server")

View File

@ -7,6 +7,14 @@ import ClairvoyantVapor
var server: SQLiteDatabase! var server: SQLiteDatabase!
private var provider: VaporMetricProvider! = nil private var provider: VaporMetricProvider! = nil
private let scheduler = EventLoopScheduler()
private var status: Metric<ServerStatus>!
private func update(status newStatus: ServerStatus) {
scheduler.schedule {
_ = try? await status.update(newStatus)
}
}
// configures your application // configures your application
public func configure(_ app: Application) async throws { public func configure(_ app: Application) async throws {
@ -18,12 +26,12 @@ public func configure(_ app: Application) async throws {
logMetricId: "schafkopf.log") logMetricId: "schafkopf.log")
MetricObserver.standard = monitor MetricObserver.standard = monitor
let status = try! await Metric<ServerStatus>( status = Metric<ServerStatus>(
"schafkopf.status", "schafkopf.status",
name: "Status", name: "Status",
description: "The main status of the server") description: "The main status of the server")
_ = try? await status.update(.initializing) update(status: .initializing)
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory) let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
.appendingPathComponent("config.json") .appendingPathComponent("config.json")
@ -32,8 +40,8 @@ public func configure(_ app: Application) async throws {
do { do {
configuration = try Configuration(loadFromUrl: configPath) configuration = try Configuration(loadFromUrl: configPath)
} catch { } catch {
_ = try? await status.update(.initializationFailure) update(status: .initializationFailure)
monitor.log("Failed to read configuration: \(error)") await monitor.log("Failed to read configuration: \(error)")
// Note: If configuration can't be loaded, then the server will run on the wrong port // Note: If configuration can't be loaded, then the server will run on the wrong port
// and access to metrics is impossible, since no tokens are loaded // and access to metrics is impossible, since no tokens are loaded
return return
@ -60,8 +68,8 @@ public func configure(_ app: Application) async throws {
do { do {
try await app.autoMigrate() try await app.autoMigrate()
} catch { } catch {
monitor.log("Failed to migrate database: \(error)") await monitor.log("Failed to migrate database: \(error)")
_ = try? await status.update(.initializationFailure) update(status: .initializationFailure)
return return
} }
@ -83,9 +91,10 @@ public func configure(_ app: Application) async throws {
// Expose metrics // Expose metrics
provider = .init(observer: monitor, accessManager: configuration.monitoringTokens) provider = .init(observer: monitor, accessManager: configuration.monitoringTokens)
provider.asyncScheduler = scheduler
provider.registerRoutes(app) provider.registerRoutes(app)
_ = try? await status.update(.nominal) update(status: .nominal)
} }
func log(_ message: String) { func log(_ message: String) {
@ -93,5 +102,7 @@ func log(_ message: String) {
print(message) print(message)
return return
} }
observer.log(message) scheduler.schedule {
await observer.log(message)
}
} }