Simplify scheduler

This commit is contained in:
Christoph Hagen 2023-12-06 10:02:43 +01:00
parent e62ccb9241
commit a08dc4f175
2 changed files with 6 additions and 29 deletions

View File

@ -1,34 +1,10 @@
import Foundation import Foundation
import Clairvoyant
import Vapor import Vapor
import NIOCore import Clairvoyant
final class EventLoopScheduler { extension MultiThreadedEventLoopGroup: AsyncScheduler {
private let backgroundGroup: EventLoopGroup public func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
_ = any().makeFutureWithTask(asyncJob)
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)
} }
} }

View File

@ -8,7 +8,7 @@ var server: SQLiteDatabase!
private var provider: VaporMetricProvider! = nil private var provider: VaporMetricProvider! = nil
private var status: Metric<ServerStatus>! private var status: Metric<ServerStatus>!
private let scheduler = EventLoopScheduler() private let scheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
private var configurationError: Error? = nil private var configurationError: Error? = nil
func configure(_ app: Application) async throws { func configure(_ app: Application) async throws {
@ -77,6 +77,7 @@ func configure(_ app: Application) async throws {
func shutdown() { func shutdown() {
scheduler.schedule { scheduler.schedule {
await server.disconnectAllSockets() await server.disconnectAllSockets()
try await scheduler.shutdownGracefully()
} }
} }