Compare commits
2 Commits
ab2a14e00b
...
18fd850413
Author | SHA1 | Date | |
---|---|---|---|
|
18fd850413 | ||
|
f60cb3c05a |
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,27 +61,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() {
|
||||
try? asyncScheduler.syncShutdownGracefully()
|
||||
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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) {
|
||||
|
Loading…
Reference in New Issue
Block a user