diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 1979931..21590a9 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -51,17 +51,15 @@ public func configure(_ app: Application) async throws { log("[\(df.string(from: Date()))] Server started") } -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)") - } +public func shutdown() async { + // Gracefully shut down by closing potentially open socket + await deviceManager.removeDeviceConnection() + do { + try await asyncScheduler.shutdownGracefully() + } catch { + print("Failed to shut down MultiThreadedEventLoopGroup: \(error)") } + print("[\(df.string(from: Date()))] Server shutdown") } private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) { diff --git a/Sources/App/entrypoint.swift b/Sources/App/entrypoint.swift index d3885da..e01f8ae 100644 --- a/Sources/App/entrypoint.swift +++ b/Sources/App/entrypoint.swift @@ -23,21 +23,29 @@ private extension Vapor.Application { @main enum Entrypoint { static func main() async throws { - var env = try Environment.detect() + var env = Environment.production try LoggingSystem.bootstrap(from: &env) let app = Application(env) - defer { - shutdown() + func cleanup() async { + await shutdown() app.shutdown() } do { try await configure(app) + await cleanup() } catch { app.logger.report(error: error) + await cleanup() + throw error + } + do { + try await app.runFromAsyncMainEntrypoint() + await cleanup() + } catch { + await cleanup() throw error } - try await app.runFromAsyncMainEntrypoint() } }