Improve logging and shutdown

This commit is contained in:
Christoph Hagen 2023-12-08 15:57:33 +01:00
parent 2e11023096
commit c25c4f3dc6
2 changed files with 20 additions and 14 deletions

View File

@ -51,17 +51,15 @@ public func configure(_ app: Application) async throws {
log("[\(df.string(from: Date()))] Server started") log("[\(df.string(from: Date()))] Server started")
} }
public func shutdown() { public func shutdown() async {
print("[\(df.string(from: Date()))] Server shutdown") // Gracefully shut down by closing potentially open socket
asyncScheduler.schedule { await deviceManager.removeDeviceConnection()
// Gracefully shut down by closing potentially open socket do {
await deviceManager.removeDeviceConnection() try await asyncScheduler.shutdownGracefully()
do { } catch {
try await asyncScheduler.shutdownGracefully() print("Failed to shut down MultiThreadedEventLoopGroup: \(error)")
} 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) { private func loadKeys(at url: URL) throws -> (deviceKey: Data, remoteKey: Data) {

View File

@ -23,21 +23,29 @@ private extension Vapor.Application {
@main @main
enum Entrypoint { enum Entrypoint {
static func main() async throws { static func main() async throws {
var env = try Environment.detect() var env = Environment.production
try LoggingSystem.bootstrap(from: &env) try LoggingSystem.bootstrap(from: &env)
let app = Application(env) let app = Application(env)
defer { func cleanup() async {
shutdown() await shutdown()
app.shutdown() app.shutdown()
} }
do { do {
try await configure(app) try await configure(app)
await cleanup()
} catch { } catch {
app.logger.report(error: error) app.logger.report(error: error)
await cleanup()
throw error
}
do {
try await app.runFromAsyncMainEntrypoint()
await cleanup()
} catch {
await cleanup()
throw error throw error
} }
try await app.runFromAsyncMainEntrypoint()
} }
} }