diff --git a/Sources/App/Config.swift b/Sources/App/Config.swift index 5b595f9..bc1db0b 100644 --- a/Sources/App/Config.swift +++ b/Sources/App/Config.swift @@ -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 + } } } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 537c949..d0aeae5 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -6,6 +6,7 @@ import ClairvoyantBinaryCodable var deviceManager: DeviceManager! private var provider: VaporMetricProvider! +private var status: Metric! 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("sesame.status") - asyncScheduler.schedule { - _ = try await status.update(.initializing) - } + status = Metric("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)") + } } }