Fix shutdown procedure

This commit is contained in:
Christoph Hagen 2023-11-10 15:08:33 +01:00
parent 621d2af22b
commit 9c85f955e4

View File

@ -9,6 +9,13 @@ private var provider: VaporMetricProvider!
private var asyncScheduler: AsyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2) private var asyncScheduler: AsyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
private let df: DateFormatter = {
let df = DateFormatter()
df.dateStyle = .short
df.timeStyle = .short
return df
}()
enum ServerError: Error { enum ServerError: Error {
case invalidAuthenticationFileContent case invalidAuthenticationFileContent
case invalidAuthenticationToken case invalidAuthenticationToken
@ -48,20 +55,18 @@ public func configure(_ app: Application) throws {
await deviceManager.updateDeviceConnectionMetric() await deviceManager.updateDeviceConnectionMetric()
} }
let df = DateFormatter()
df.dateStyle = .short
df.timeStyle = .short
print("[\(df.string(from: Date()))] Server started") print("[\(df.string(from: Date()))] Server started")
// Gracefully shut down by closing potentially open socket // Gracefully shut down by closing potentially open socket
//DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) { // Must be done after app is running, otherwise error is thrown
_ = app.server.onShutdown.always { _ in DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) {
print("[\(df.string(from: Date()))] Server shutdown") _ = app.server.onShutdown.always { _ in
asyncScheduler.schedule { print("[\(df.string(from: Date()))] Server shutdown")
await deviceManager.removeDeviceConnection() asyncScheduler.schedule {
await deviceManager.removeDeviceConnection()
}
} }
} }
//}
} }
private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) { private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) {