Switch to new Vapor main

This commit is contained in:
Christoph Hagen
2023-12-06 10:01:19 +01:00
parent 1e649f297e
commit e62ccb9241
5 changed files with 55 additions and 67 deletions

View File

@ -11,23 +11,7 @@ private var status: Metric<ServerStatus>!
private let scheduler = EventLoopScheduler()
private var configurationError: Error? = nil
public func configure(_ app: Application) throws {
let semaphore = DispatchSemaphore(value: 0)
scheduler.schedule {
do {
try await configureAsync(app)
} catch {
configurationError = error
}
semaphore.signal()
}
semaphore.wait()
if let configurationError {
throw configurationError
}
}
private func configureAsync(_ app: Application) async throws {
func configure(_ app: Application) async throws {
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
@ -45,7 +29,7 @@ private func configureAsync(_ app: Application) async throws {
name: "Status",
description: "The main status of the server")
_ = try? await status.update(.initializing)
try await status.update(.initializing)
app.http.server.configuration.port = configuration.serverPort
@ -69,7 +53,7 @@ private func configureAsync(_ app: Application) async throws {
try await app.autoMigrate()
} catch {
await monitor.log("Failed to migrate database: \(error)")
_ = try? await status.update(.initializationFailure)
try await status.update(.initializationFailure)
return
}
@ -79,15 +63,6 @@ private func configureAsync(_ app: Application) async throws {
let db = app.databases.database(.sqlite, logger: .init(label: "Init"), on: app.databases.eventLoopGroup.next())!
server = try await SQLiteDatabase(database: db, mail: configuration.mail)
// Gracefully shut down by closing potentially open socket
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) {
_ = app.server.onShutdown.always { _ in
scheduler.schedule {
await server.disconnectAllSockets()
}
}
}
// register routes
routes(app)
@ -96,7 +71,13 @@ private func configureAsync(_ app: Application) async throws {
provider.asyncScheduler = scheduler
provider.registerRoutes(app)
_ = try? await status.update(.nominal)
try await status.update(.nominal)
}
func shutdown() {
scheduler.schedule {
await server.disconnectAllSockets()
}
}
func log(_ message: String) {

View File

@ -0,0 +1,43 @@
import Vapor
import Dispatch
import Logging
/// This extension is temporary and can be removed once Vapor gets this support.
private extension Vapor.Application {
static let baseExecutionQueue = DispatchQueue(label: "vapor.codes.entrypoint")
func runFromAsyncMainEntrypoint() async throws {
try await withCheckedThrowingContinuation { continuation in
Vapor.Application.baseExecutionQueue.async { [self] in
do {
try self.run()
continuation.resume()
} catch {
continuation.resume(throwing: error)
}
}
}
}
}
@main
enum Entrypoint {
static func main() async throws {
var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)
let app = Application(env)
defer {
shutdown()
app.shutdown()
}
do {
try await configure(app)
} catch {
app.logger.report(error: error)
throw error
}
try await app.runFromAsyncMainEntrypoint()
}
}

View File

@ -1,10 +0,0 @@
import App
import Vapor
var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)
let app = Application(env)
defer { app.shutdown() }
try configure(app)
try app.run()