Improve startup

This commit is contained in:
Christoph Hagen 2023-11-22 10:02:16 +01:00
parent 848ff21134
commit 14f06072ad
3 changed files with 27 additions and 15 deletions

View File

@ -172,7 +172,6 @@ final class CapServer {
log("Failed to load caps: \(error)") log("Failed to load caps: \(error)")
throw error throw error
} }
log("\(caps.count) caps loaded")
} }
private func scheduleSave() { private func scheduleSave() {

View File

@ -5,10 +5,19 @@ import ClairvoyantVapor
import ClairvoyantBinaryCodable import ClairvoyantBinaryCodable
private var provider: VaporMetricProvider! private var provider: VaporMetricProvider!
private var serverStatus: Metric<ServerStatus>!
private let asyncScheduler = EventLoopScheduler() 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 resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory)
let publicDirectory = app.directory.publicDirectory 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") let monitor = MetricObserver(logFileFolder: config.logURL, logMetricId: "caps.log")
MetricObserver.standard = monitor MetricObserver.standard = monitor
let status = Metric<ServerStatus>("caps.status", serverStatus = Metric<ServerStatus>("caps.status",
name: "Status", name: "Status",
description: "The general status of the service") description: "The general status of the service")
try await status.update(.initializing) status(.initializing)
app.http.server.configuration.port = config.port app.http.server.configuration.port = config.port
app.routes.defaultMaxBodySize = .init(stringLiteral: config.maxBodySize) 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 = .init(observer: monitor, accessManager: config.writers)
provider.asyncScheduler = asyncScheduler provider.asyncScheduler = asyncScheduler
@ -45,13 +54,16 @@ public func configure(_ app: Application) async throws {
do { do {
try server.loadData() try server.loadData()
} catch { } catch {
try await status.update(.initializationFailure) status(.initializationFailure)
print("[\(df.string(from: Date()))] Server failed to start: \(error)")
return
} }
if server.canResizeImages { if server.canResizeImages {
try await status.update(.nominal) status(.nominal)
} else { } else {
try await status.update(.reducedFunctionality) status(.reducedFunctionality)
} }
print("[\(df.string(from: Date()))] Server started (\(server.capCount) caps)")
} }
func log(_ message: String) { func log(_ message: String) {
@ -63,3 +75,10 @@ func log(_ message: String) {
await observer.log(message) await observer.log(message)
} }
} }
private let df: DateFormatter = {
let df = DateFormatter()
df.dateStyle = .short
df.timeStyle = .short
return df
}()

View File

@ -5,11 +5,5 @@ var env = Environment.production
try LoggingSystem.bootstrap(from: &env) try LoggingSystem.bootstrap(from: &env)
let app = Application(env) let app = Application(env)
defer { app.shutdown() } defer { app.shutdown() }
try configure(app)
private let semaphore = DispatchSemaphore(value: 0)
Task {
try await configure(app)
semaphore.signal()
}
semaphore.wait()
try app.run() try app.run()