1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-05 20:40:05 +01:00

♻️ Avoid using non-Sendable Timer

This commit is contained in:
2024-08-06 14:15:37 +02:00
parent e3ea712a99
commit 0c320074da

View File

@@ -8,9 +8,6 @@
import Foundation import Foundation
extension Process: @unchecked Sendable {}
extension Timer: @unchecked Sendable {}
class RealShell: ShellProtocol { class RealShell: ShellProtocol {
/** /**
The launch path of the terminal in question that is used. The launch path of the terminal in question that is used.
@@ -184,25 +181,26 @@ class RealShell: ShellProtocol {
} }
return try await withCheckedThrowingContinuation({ continuation in return try await withCheckedThrowingContinuation({ continuation in
let timer = Timer.scheduledTimer(withTimeInterval: timeout, repeats: false) { _ in let task = Task {
try await Task.sleep(nanoseconds: timeout.nanoseconds)
// Only terminate if the process is still running // Only terminate if the process is still running
if process.isRunning { if process.isRunning {
process.terminationHandler = nil process.terminationHandler = nil
process.terminate() process.terminate()
return continuation.resume(throwing: ShellError.timedOut) continuation.resume(throwing: ShellError.timedOut)
} }
} }
process.terminationHandler = { [timer, output] process in process.terminationHandler = { [output] process in
timer.invalidate() task.cancel()
process.haltListening() process.haltListening()
if !output.err.isEmpty { if !output.err.isEmpty {
return continuation.resume(returning: (process, .err(output.err))) continuation.resume(returning: (process, .err(output.err)))
} else {
continuation.resume(returning: (process, .out(output.out)))
} }
return continuation.resume(returning: (process, .out(output.out)))
} }
process.launch() process.launch()
@@ -210,3 +208,9 @@ class RealShell: ShellProtocol {
}) })
} }
} }
extension TimeInterval {
var nanoseconds: UInt64 {
return UInt64(self * 1_000_000_000)
}
}