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")
}
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) {

View File

@ -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()
}
}