From 14f06072adccb83467bf40e0736bca8cc4d38155 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 22 Nov 2023 10:02:16 +0100 Subject: [PATCH] Improve startup --- Sources/App/CapServer.swift | 1 - Sources/App/configure.swift | 33 ++++++++++++++++++++++++++------- Sources/Run/main.swift | 8 +------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Sources/App/CapServer.swift b/Sources/App/CapServer.swift index 46aa813..9735f0a 100644 --- a/Sources/App/CapServer.swift +++ b/Sources/App/CapServer.swift @@ -172,7 +172,6 @@ final class CapServer { log("Failed to load caps: \(error)") throw error } - log("\(caps.count) caps loaded") } private func scheduleSave() { diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 6cab2bb..18fa843 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -5,10 +5,19 @@ import ClairvoyantVapor import ClairvoyantBinaryCodable private var provider: VaporMetricProvider! +private var serverStatus: Metric! private let asyncScheduler = EventLoopScheduler() -public func configure(_ app: Application) async throws { +private var server: CapServer! + +private func status(_ newStatus: ServerStatus) { + asyncScheduler.schedule { + try await serverStatus.update(newStatus) + } +} + +public func configure(_ app: Application) throws { let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory) let publicDirectory = app.directory.publicDirectory @@ -19,15 +28,15 @@ public func configure(_ app: Application) async throws { let monitor = MetricObserver(logFileFolder: config.logURL, logMetricId: "caps.log") MetricObserver.standard = monitor - let status = Metric("caps.status", + serverStatus = Metric("caps.status", name: "Status", description: "The general status of the service") - try await status.update(.initializing) + status(.initializing) app.http.server.configuration.port = config.port app.routes.defaultMaxBodySize = .init(stringLiteral: config.maxBodySize) - let server = CapServer(in: URL(fileURLWithPath: publicDirectory)) + server = CapServer(in: URL(fileURLWithPath: publicDirectory)) provider = .init(observer: monitor, accessManager: config.writers) provider.asyncScheduler = asyncScheduler @@ -45,13 +54,16 @@ public func configure(_ app: Application) async throws { do { try server.loadData() } catch { - try await status.update(.initializationFailure) + status(.initializationFailure) + print("[\(df.string(from: Date()))] Server failed to start: \(error)") + return } if server.canResizeImages { - try await status.update(.nominal) + status(.nominal) } else { - try await status.update(.reducedFunctionality) + status(.reducedFunctionality) } + print("[\(df.string(from: Date()))] Server started (\(server.capCount) caps)") } func log(_ message: String) { @@ -63,3 +75,10 @@ func log(_ message: String) { await observer.log(message) } } + +private let df: DateFormatter = { + let df = DateFormatter() + df.dateStyle = .short + df.timeStyle = .short + return df +}() diff --git a/Sources/Run/main.swift b/Sources/Run/main.swift index 05937c6..e715f81 100755 --- a/Sources/Run/main.swift +++ b/Sources/Run/main.swift @@ -5,11 +5,5 @@ var env = Environment.production try LoggingSystem.bootstrap(from: &env) let app = Application(env) defer { app.shutdown() } - -private let semaphore = DispatchSemaphore(value: 0) -Task { - try await configure(app) - semaphore.signal() -} -semaphore.wait() +try configure(app) try app.run()