Simplify async scheduler
This commit is contained in:
parent
1fd63b8cc3
commit
6aaa9cb458
@ -3,32 +3,9 @@ import Clairvoyant
|
||||
import Vapor
|
||||
import NIOCore
|
||||
|
||||
final class EventLoopScheduler {
|
||||
extension MultiThreadedEventLoopGroup: AsyncScheduler {
|
||||
|
||||
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)
|
||||
public func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
|
||||
_ = any().makeFutureWithTask(asyncJob)
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import ClairvoyantBinaryCodable
|
||||
private var provider: VaporMetricProvider!
|
||||
private var serverStatus: Metric<ServerStatus>!
|
||||
|
||||
private let asyncScheduler = EventLoopScheduler()
|
||||
private let asyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
|
||||
|
||||
private var server: CapServer!
|
||||
|
||||
@ -67,6 +67,17 @@ public func configure(_ app: Application) throws {
|
||||
print("[\(df.string(from: Date()))] Server started (\(server.capCount) caps)")
|
||||
}
|
||||
|
||||
public 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)
|
||||
|
@ -4,6 +4,9 @@ import Vapor
|
||||
var env = Environment.production
|
||||
try LoggingSystem.bootstrap(from: &env)
|
||||
let app = Application(env)
|
||||
defer { app.shutdown() }
|
||||
defer {
|
||||
shutdown()
|
||||
app.shutdown()
|
||||
}
|
||||
try configure(app)
|
||||
try app.run()
|
||||
|
Loading…
Reference in New Issue
Block a user