diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 1c3c5ec..e95ee02 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -7,7 +7,7 @@ var deviceManager: DeviceManager! private var provider: VaporMetricProvider! -private var asyncScheduler: AsyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2) +private var asyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2) private let df: DateFormatter = { let df = DateFormatter() @@ -57,7 +57,8 @@ public func configure(_ app: Application) throws { } print("[\(df.string(from: Date()))] Server started") - + 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)) { @@ -70,6 +71,10 @@ public func configure(_ app: Application) throws { } } +public func shutdown() { + try? asyncScheduler.syncShutdownGracefully() +} + private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) { let authContent: [Data] = try String(contentsOf: url) .trimmingCharacters(in: .whitespacesAndNewlines) diff --git a/Sources/Run/main.swift b/Sources/Run/main.swift index 5a1383a..3cc5310 100644 --- a/Sources/Run/main.swift +++ b/Sources/Run/main.swift @@ -4,7 +4,10 @@ import Vapor var env = Environment.production //.detect() try LoggingSystem.bootstrap(from: &env) let app = Application(env) -defer { app.shutdown() } +defer { + app.shutdown() + shutdown() +} try configure(app) try app.run()