@@ -6,25 +6,19 @@ import de.lobbenmeier.stefan.settings.business.Settings
6
6
import de.lobbenmeier.stefan.updater.business.getPlatform
7
7
import de.lobbenmeier.stefan.updater.model.Binaries
8
8
import kotlin.io.path.pathString
9
- import kotlinx.coroutines.CoroutineScope
10
- import kotlinx.coroutines.Dispatchers
11
- import kotlinx.coroutines.launch
12
9
import kotlinx.coroutines.sync.Semaphore
13
10
import kotlinx.coroutines.sync.withPermit
14
11
15
12
class YtDlp (private val binaries : Binaries , private val settings : Settings ) {
16
13
17
- private val semaphore = Semaphore (settings.rateLimit ?.toInt() ? : 100 )
14
+ private val semaphore = Semaphore (settings.maxConcurrentJobs ?.toInt() ? : 100 )
18
15
19
16
fun createDownloadItem (url : String ): DownloadItem {
20
17
return DownloadItem (this , url).also { it.gatherMetadata() }
21
18
}
22
19
23
- fun run (vararg options : String ) {
24
- CoroutineScope (Dispatchers .IO ).launch { runAsync(* options) }
25
- }
26
-
27
20
suspend fun runAsync (
21
+ isDownloadJob : Boolean ,
28
22
vararg options : String ,
29
23
consumer : suspend (String ) -> Unit = { line -> println("process $line") }
30
24
) {
@@ -46,7 +40,7 @@ class YtDlp(private val binaries: Binaries, private val settings: Settings) {
46
40
val command = arrayOf(ytDlpBinary, * fullOptions).joinToString(separator = " " ) { " \" $it \" " }
47
41
48
42
val res =
49
- semaphore. withPermit {
43
+ withPermit(isDownloadJob) {
50
44
println (" Start process: $command " )
51
45
52
46
process(
@@ -67,4 +61,15 @@ class YtDlp(private val binaries: Binaries, private val settings: Settings) {
67
61
throw Exception (" yt-dlp indicated error in its response" )
68
62
}
69
63
}
64
+
65
+ private suspend fun <T > withPermit (
66
+ usePermit : Boolean ,
67
+ action : suspend () -> T ,
68
+ ): T {
69
+ return if (usePermit) {
70
+ semaphore.withPermit { action() }
71
+ } else {
72
+ action()
73
+ }
74
+ }
70
75
}
0 commit comments