33 lines
928 B
Swift
33 lines
928 B
Swift
//
|
|
// SDLSupervisor.swift
|
|
// punchnet
|
|
//
|
|
// Created by 安礼成 on 2026/3/10.
|
|
//
|
|
|
|
actor SDLSupervisor {
|
|
private var loopChildWorkers: [Task<Void, Never>] = []
|
|
|
|
func addWorker(name: String, _ body: @escaping () async throws -> Void, retryDelay: Duration = .seconds(2)) {
|
|
let worker = Task(name: name) {
|
|
while !Task.isCancelled {
|
|
do {
|
|
try await body()
|
|
} catch is CancellationError {
|
|
break
|
|
} catch let err {
|
|
SDLLogger.log("[Supervisor] worker \(name) crashed: \(err.localizedDescription)", for: .debug)
|
|
try? await Task.sleep(for: retryDelay)
|
|
}
|
|
}
|
|
}
|
|
self.loopChildWorkers.append(worker)
|
|
}
|
|
|
|
func stop() {
|
|
self.loopChildWorkers.forEach { $0.cancel() }
|
|
self.loopChildWorkers.removeAll()
|
|
}
|
|
|
|
}
|