Improve logging of errors

This commit is contained in:
Christoph Hagen 2023-11-28 11:26:43 +01:00
parent f60cb3c05a
commit 18fd850413
2 changed files with 26 additions and 11 deletions

View File

@ -39,21 +39,21 @@ extension Config {
init(loadFrom url: URL) throws {
guard FileManager.default.fileExists(atPath: url.path) else {
log("No configuration file found at \(url.path)")
print("No configuration file found at \(url.path)")
fatalError("No configuration file found")
}
let data: Data
do {
data = try Data(contentsOf: url)
} catch {
log("Failed to read config data: \(error)")
print("Failed to read config data: \(error)")
throw error
}
do {
self = try JSONDecoder().decode(Config.self, from: data)
} catch {
log("Failed to decode config data: \(error)")
throw error
}
print("Failed to decode config data: \(error)")
throw error
}
}
}

View File

@ -6,6 +6,7 @@ import ClairvoyantBinaryCodable
var deviceManager: DeviceManager!
private var provider: VaporMetricProvider!
private var status: Metric<ServerStatus>!
private var asyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
@ -21,6 +22,16 @@ enum ServerError: Error {
case invalidAuthenticationToken
}
private func updateStatus(_ newStatus: ServerStatus) {
asyncScheduler.schedule {
do {
try await status.update(newStatus)
} catch {
print("Failed to update server status: \(error)")
}
}
}
// configures your application
public func configure(_ app: Application) throws {
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
@ -33,10 +44,8 @@ public func configure(_ app: Application) throws {
let monitor = MetricObserver(logFileFolder: logFolder, logMetricId: "sesame.log")
MetricObserver.standard = monitor
let status = Metric<ServerStatus>("sesame.status")
asyncScheduler.schedule {
_ = try await status.update(.initializing)
}
status = Metric<ServerStatus>("sesame.status")
updateStatus(.initializing)
app.http.server.configuration.port = config.port
@ -52,8 +61,10 @@ public func configure(_ app: Application) throws {
provider.registerRoutes(app)
monitor.saveCurrentListOfMetricsToLogFolder()
updateStatus(.nominal)
// Update the metric of the device status to ensure that it is accurate
asyncScheduler.schedule {
_ = try await status.update(.nominal)
await deviceManager.updateDeviceConnectionMetric()
}
@ -65,7 +76,11 @@ public func shutdown() {
asyncScheduler.schedule {
// Gracefully shut down by closing potentially open socket
await deviceManager.removeDeviceConnection()
try? await asyncScheduler.shutdownGracefully()
do {
try await asyncScheduler.shutdownGracefully()
} catch {
print("Failed to shut down MultiThreadedEventLoopGroup: \(error)")
}
}
}