From 9c85f955e4d5de7cf8764a4aeae039c207460ef0 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Fri, 10 Nov 2023 15:08:33 +0100 Subject: [PATCH] Fix shutdown procedure --- Sources/App/configure.swift | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 0f7709d..a175e3b 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -9,6 +9,13 @@ private var provider: VaporMetricProvider! 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 { case invalidAuthenticationFileContent case invalidAuthenticationToken @@ -48,20 +55,18 @@ public func configure(_ app: Application) throws { await deviceManager.updateDeviceConnectionMetric() } - let df = DateFormatter() - df.dateStyle = .short - df.timeStyle = .short print("[\(df.string(from: Date()))] Server started") // Gracefully shut down by closing potentially open socket - //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() + // 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() + } } } - //} } private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) {