Compare commits
No commits in common. "e6132a38b3df9c86085a902caefb94c8f727e192" and "1fd63b8cc3c3604cd7922904905f3764fecd45cf" have entirely different histories.
e6132a38b3
...
1fd63b8cc3
@ -13,15 +13,21 @@ let package = Package(
|
||||
.package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.0"),
|
||||
],
|
||||
targets: [
|
||||
.executableTarget(
|
||||
name: "App",
|
||||
.target(name: "App",
|
||||
dependencies: [
|
||||
.product(name: "Vapor", package: "vapor"),
|
||||
.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#building-for-production> for details.
|
||||
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
|
||||
]),
|
||||
.executableTarget(name: "Run", dependencies: ["App"]),
|
||||
.testTarget(name: "AppTests", dependencies: ["App"]),
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -3,9 +3,32 @@ import Clairvoyant
|
||||
import Vapor
|
||||
import NIOCore
|
||||
|
||||
extension MultiThreadedEventLoopGroup: AsyncScheduler {
|
||||
final class EventLoopScheduler {
|
||||
|
||||
public func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
|
||||
_ = any().makeFutureWithTask(asyncJob)
|
||||
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
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension EventLoopScheduler: AsyncScheduler {
|
||||
|
||||
func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
|
||||
_ = backgroundGroup.any().makeFutureWithTask(asyncJob)
|
||||
}
|
||||
}
|
||||
|
@ -7,11 +7,17 @@ import ClairvoyantBinaryCodable
|
||||
private var provider: VaporMetricProvider!
|
||||
private var serverStatus: Metric<ServerStatus>!
|
||||
|
||||
private let asyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
|
||||
private let asyncScheduler = EventLoopScheduler()
|
||||
|
||||
private var server: CapServer!
|
||||
|
||||
func configure(_ app: Application) async throws {
|
||||
private func status(_ newStatus: ServerStatus) {
|
||||
asyncScheduler.schedule {
|
||||
try await serverStatus.update(newStatus)
|
||||
}
|
||||
}
|
||||
|
||||
public func configure(_ app: Application) throws {
|
||||
|
||||
let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||
let publicDirectory = app.directory.publicDirectory
|
||||
@ -26,7 +32,7 @@ func configure(_ app: Application) async throws {
|
||||
serverStatus = Metric<ServerStatus>("caps.status",
|
||||
name: "Status",
|
||||
description: "The general status of the service")
|
||||
try await serverStatus.update(.initializing)
|
||||
status(.initializing)
|
||||
|
||||
app.http.server.configuration.port = config.port
|
||||
app.routes.defaultMaxBodySize = .init(stringLiteral: config.maxBodySize)
|
||||
@ -49,29 +55,18 @@ func configure(_ app: Application) async throws {
|
||||
do {
|
||||
try server.loadData()
|
||||
} catch {
|
||||
try await serverStatus.update(.initializationFailure)
|
||||
status(.initializationFailure)
|
||||
print("[\(df.string(from: Date()))] Server failed to start: \(error)")
|
||||
return
|
||||
}
|
||||
if server.canResizeImages {
|
||||
try await serverStatus.update(.nominal)
|
||||
status(.nominal)
|
||||
} else {
|
||||
try await serverStatus.update(.reducedFunctionality)
|
||||
status(.reducedFunctionality)
|
||||
}
|
||||
print("[\(df.string(from: Date()))] Server started (\(server.capCount) caps)")
|
||||
}
|
||||
|
||||
func shutdown() {
|
||||
print("[\(df.string(from: Date()))] Server shutdown")
|
||||
asyncScheduler.schedule {
|
||||
do {
|
||||
try await asyncScheduler.shutdownGracefully()
|
||||
} catch {
|
||||
print("Failed to shut down MultiThreadedEventLoopGroup: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func log(_ message: String) {
|
||||
guard let observer = MetricObserver.standard else {
|
||||
print(message)
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
9
Sources/Run/main.swift
Executable file
9
Sources/Run/main.swift
Executable file
@ -0,0 +1,9 @@
|
||||
import App
|
||||
import Vapor
|
||||
|
||||
var env = Environment.production
|
||||
try LoggingSystem.bootstrap(from: &env)
|
||||
let app = Application(env)
|
||||
defer { app.shutdown() }
|
||||
try configure(app)
|
||||
try app.run()
|
0
Tests/.gitkeep
Executable file
0
Tests/.gitkeep
Executable file
13
Tests/AppTests/AppTests.swift
Executable file
13
Tests/AppTests/AppTests.swift
Executable file
@ -0,0 +1,13 @@
|
||||
import App
|
||||
import Dispatch
|
||||
import XCTest
|
||||
|
||||
final class AppTests : XCTestCase {
|
||||
func testNothing() throws {
|
||||
XCTAssert(true)
|
||||
}
|
||||
|
||||
static let allTests = [
|
||||
("testNothing", testNothing),
|
||||
]
|
||||
}
|
0
Tests/LinuxMain.swift
Executable file
0
Tests/LinuxMain.swift
Executable file
Loading…
Reference in New Issue
Block a user