Compare commits

..

No commits in common. "18fd850413ca95d0e37baf842bf06c48388e5333" and "ab2a14e00b0c5cc042d220ed10fb7ef59bd18035" have entirely different histories.

2 changed files with 22 additions and 31 deletions

View File

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

View File

@ -6,7 +6,6 @@ import ClairvoyantBinaryCodable
var deviceManager: DeviceManager!
private var provider: VaporMetricProvider!
private var status: Metric<ServerStatus>!
private var asyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
@ -22,16 +21,6 @@ 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)
@ -44,8 +33,10 @@ public func configure(_ app: Application) throws {
let monitor = MetricObserver(logFileFolder: logFolder, logMetricId: "sesame.log")
MetricObserver.standard = monitor
status = Metric<ServerStatus>("sesame.status")
updateStatus(.initializing)
let status = Metric<ServerStatus>("sesame.status")
asyncScheduler.schedule {
_ = try await status.update(.initializing)
}
app.http.server.configuration.port = config.port
@ -61,27 +52,27 @@ 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()
}
log("[\(df.string(from: Date()))] Server started")
// Gracefully shut down by closing potentially open socket
// Must be done after app is running, otherwise error is thrown
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) {
_ = app.server.onShutdown.always { _ in
print("[\(df.string(from: Date()))] Server shutdown")
asyncScheduler.schedule {
await deviceManager.removeDeviceConnection()
}
}
}
}
public func shutdown() {
print("[\(df.string(from: Date()))] Server shutdown")
asyncScheduler.schedule {
// Gracefully shut down by closing potentially open socket
await deviceManager.removeDeviceConnection()
do {
try await asyncScheduler.shutdownGracefully()
} catch {
print("Failed to shut down MultiThreadedEventLoopGroup: \(error)")
}
}
try? asyncScheduler.syncShutdownGracefully()
}
private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) {