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 { init(loadFrom url: URL) throws {
guard FileManager.default.fileExists(atPath: url.path) else { 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") fatalError("No configuration file found")
} }
let data: Data let data: Data
do { do {
data = try Data(contentsOf: url) data = try Data(contentsOf: url)
} catch { } catch {
log("Failed to read config data: \(error)") print("Failed to read config data: \(error)")
throw error throw error
} }
do { do {
self = try JSONDecoder().decode(Config.self, from: data) self = try JSONDecoder().decode(Config.self, from: data)
} catch { } catch {
log("Failed to decode config data: \(error)") print("Failed to decode config data: \(error)")
throw error throw error
} }
} }
} }

View File

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