Compare commits
No commits in common. "a08dc4f175b27422366ccb9e82dee23a8ccd84ad" and "1e649f297e25add6033694a17eb28ac296adeb18" have entirely different histories.
a08dc4f175
...
1e649f297e
@ -20,7 +20,7 @@ let package = Package(
|
||||
.package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"),
|
||||
],
|
||||
targets: [
|
||||
.executableTarget(
|
||||
.target(
|
||||
name: "App",
|
||||
dependencies: [
|
||||
.product(name: "Fluent", package: "fluent"),
|
||||
@ -30,7 +30,18 @@ let package = Package(
|
||||
.product(name: "Clairvoyant", package: "Clairvoyant"),
|
||||
.product(name: "ClairvoyantVapor", package: "ClairvoyantVapor"),
|
||||
.product(name: "ClairvoyantBinaryCodable", package: "ClairvoyantBinaryCodable"),
|
||||
]
|
||||
)
|
||||
],
|
||||
swiftSettings: [
|
||||
// Enable better optimizations when building in Release configuration. Despite the use of
|
||||
// the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
|
||||
// builds. See <https://github.com/swift-server/guides/blob/main/docs/building.md#building-for-production> for details.
|
||||
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
|
||||
]
|
||||
),
|
||||
.executableTarget(name: "Run", dependencies: [.target(name: "App")]),
|
||||
// .testTarget(name: "AppTests", dependencies: [
|
||||
// .target(name: "App"),
|
||||
// .product(name: "XCTVapor", package: "vapor"),
|
||||
// ])
|
||||
]
|
||||
)
|
||||
|
@ -1,10 +1,34 @@
|
||||
import Foundation
|
||||
import Vapor
|
||||
import Clairvoyant
|
||||
import Vapor
|
||||
import NIOCore
|
||||
|
||||
extension MultiThreadedEventLoopGroup: AsyncScheduler {
|
||||
final class EventLoopScheduler {
|
||||
|
||||
private let backgroundGroup: EventLoopGroup
|
||||
|
||||
init(numberOfThreads: Int = 2) {
|
||||
backgroundGroup = MultiThreadedEventLoopGroup(numberOfThreads: numberOfThreads)
|
||||
}
|
||||
|
||||
func next() -> EventLoop {
|
||||
backgroundGroup.next()
|
||||
}
|
||||
|
||||
func provider() -> NIOEventLoopGroupProvider {
|
||||
return .shared(backgroundGroup)
|
||||
}
|
||||
|
||||
func shutdown() {
|
||||
backgroundGroup.shutdownGracefully { _ in
|
||||
|
||||
public func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
|
||||
_ = any().makeFutureWithTask(asyncJob)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension EventLoopScheduler: AsyncScheduler {
|
||||
|
||||
func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
|
||||
_ = backgroundGroup.any().makeFutureWithTask(asyncJob)
|
||||
}
|
||||
}
|
||||
|
@ -8,10 +8,26 @@ var server: SQLiteDatabase!
|
||||
|
||||
private var provider: VaporMetricProvider! = nil
|
||||
private var status: Metric<ServerStatus>!
|
||||
private let scheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
|
||||
private let scheduler = EventLoopScheduler()
|
||||
private var configurationError: Error? = nil
|
||||
|
||||
func configure(_ app: Application) async throws {
|
||||
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 {
|
||||
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||
|
||||
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||
@ -29,7 +45,7 @@ func configure(_ 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
|
||||
|
||||
@ -53,7 +69,7 @@ func configure(_ 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
|
||||
}
|
||||
|
||||
@ -63,6 +79,15 @@ func configure(_ 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)
|
||||
|
||||
@ -71,14 +96,7 @@ func configure(_ app: Application) async throws {
|
||||
provider.asyncScheduler = scheduler
|
||||
provider.registerRoutes(app)
|
||||
|
||||
try await status.update(.nominal)
|
||||
}
|
||||
|
||||
func shutdown() {
|
||||
scheduler.schedule {
|
||||
await server.disconnectAllSockets()
|
||||
try await scheduler.shutdownGracefully()
|
||||
}
|
||||
_ = try? await status.update(.nominal)
|
||||
}
|
||||
|
||||
func log(_ message: String) {
|
||||
|
@ -1,43 +0,0 @@
|
||||
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()
|
||||
}
|
||||
}
|
10
Sources/Run/main.swift
Normal file
10
Sources/Run/main.swift
Normal file
@ -0,0 +1,10 @@
|
||||
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()
|
15
Tests/AppTests/AppTests.swift
Normal file
15
Tests/AppTests/AppTests.swift
Normal file
@ -0,0 +1,15 @@
|
||||
@testable import App
|
||||
import XCTVapor
|
||||
|
||||
final class AppTests: XCTestCase {
|
||||
func testHelloWorld() throws {
|
||||
let app = Application(.testing)
|
||||
defer { app.shutdown() }
|
||||
try configure(app)
|
||||
|
||||
try app.test(.GET, "hello", afterResponse: { res in
|
||||
XCTAssertEqual(res.status, .ok)
|
||||
XCTAssertEqual(res.body.string, "Hello, world!")
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user