Caps-Server/Sources/App/EventLoopScheduler.swift

35 lines
734 B
Swift
Raw Normal View History

2023-10-02 00:04:36 +02:00
import Foundation
import Clairvoyant
import Vapor
import NIOCore
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
}
}
}
extension EventLoopScheduler: AsyncScheduler {
func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
_ = backgroundGroup.any().makeFutureWithTask(asyncJob)
}
}