From 04248b04af6c4bad17ec16424d98206227a36b18 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Thu, 7 Sep 2023 14:13:28 +0200 Subject: [PATCH] Update clairvoyant --- Package.swift | 7 +++++-- Sources/App/DeviceManager.swift | 2 +- Sources/App/configure.swift | 25 +++++++++++-------------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Package.swift b/Package.swift index b516923..8f28cbd 100644 --- a/Package.swift +++ b/Package.swift @@ -8,7 +8,9 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"), - .package(url: "https://github.com/christophhagen/clairvoyant.git", from: "0.5.0"), + .package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.9.0"), + .package(url: "https://github.com/christophhagen/ClairvoyantVapor", from: "0.2.0"), + .package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"), ], targets: [ .target( @@ -16,7 +18,8 @@ let package = Package( dependencies: [ .product(name: "Vapor", package: "vapor"), .product(name: "Clairvoyant", package: "Clairvoyant"), - + .product(name: "ClairvoyantVapor", package: "ClairvoyantVapor"), + .product(name: "ClairvoyantBinaryCodable", package: "ClairvoyantBinaryCodable"), ], swiftSettings: [ // Enable better optimizations when building in Release configuration. Despite the use of diff --git a/Sources/App/DeviceManager.swift b/Sources/App/DeviceManager.swift index 4950b9e..471546c 100644 --- a/Sources/App/DeviceManager.swift +++ b/Sources/App/DeviceManager.swift @@ -56,7 +56,7 @@ final class DeviceManager { private func updateMessageCountMetric() { Task { let lastValue = await messagesToDeviceMetric.lastValue()?.value ?? 0 - try? await messagesToDeviceMetric.update(lastValue + 1) + _ = try? await messagesToDeviceMetric.update(lastValue + 1) } } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 194b45b..8d7b00a 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -1,8 +1,12 @@ import Vapor import Clairvoyant +import ClairvoyantVapor +import ClairvoyantBinaryCodable var deviceManager: DeviceManager! +private var provider: VaporMetricProvider! + enum ServerError: Error { case invalidAuthenticationFileContent case invalidAuthenticationToken @@ -20,23 +24,15 @@ public func configure(_ app: Application) async throws { let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) let logFolder = storageFolder.appendingPathComponent("logs") - let accessManager = AccessTokenManager([]) - let monitor = await MetricObserver( - logFolder: logFolder, - accessManager: accessManager, - logMetricId: "sesame.log") + let monitor = MetricObserver(logFileFolder: logFolder, logMetricId: "sesame.log") MetricObserver.standard = monitor let status = try await Metric("sesame.status") - try await status.update(.initializing) - - await monitor.registerRoutes(app) + _ = try await status.update(.initializing) let configUrl = storageFolder.appendingPathComponent("config.json") let config = try Config(loadFrom: configUrl) - config.authenticationTokens.map { $0.data(using: .utf8)! }.forEach(accessManager.add) - app.http.server.configuration.port = config.port let keyFile = storageFolder.appendingPathComponent(config.keyFileName) @@ -48,6 +44,9 @@ public func configure(_ app: Application) async throws { deviceTimeout: config.deviceTimeout) try routes(app) + + provider = .init(observer: monitor, accessManager: config.authenticationTokens) + provider.registerRoutes(app) // Gracefully shut down by closing potentially open socket DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) { @@ -56,7 +55,7 @@ public func configure(_ app: Application) async throws { } } - try await status.update(.nominal) + _ = try await status.update(.nominal) print("[\(dateFormatter.string(from: Date()))] Server started") } @@ -85,7 +84,5 @@ func log(_ message: String) { print(message) return } - Task { - await observer.log(message) - } + observer.log(message) }