blob: 75fefdf7e156230f87cf16faa953a86e83de1d57 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import Logging
import ServiceLifecycle
import _NIOFileSystem
struct Watcher: Service {
let directory: FilePath
let logger: Logger
let suffix: String
func foundDownloads() async throws -> Bool {
try await FileSystem.shared.withDirectoryHandle(atPath: self.directory) {
try await $0.listContents().contains {
$0.name.string.hasSuffix(self.suffix)
}
}
}
func run() async throws {
while !Task.isCancelled {
do {
while try await !foundDownloads() {
try await Task.sleep(for: .seconds(30))
}
try await withSleepInhibited {
self.logger.debug("Ongoing downloads found, inhibiting sleep")
while try await foundDownloads() {
try await Task.sleep(for: .seconds(30))
}
self.logger.debug("No ongoing downloads found, allowing sleep")
}
} catch is CancellationError {
return
}
}
}
}
|