Commit 7ed5dd16 authored by Him188's avatar Him188

Add retry for cuiCloudUpload

parent c433360d
......@@ -21,6 +21,9 @@ import org.gradle.api.Project
import org.gradle.kotlin.dsl.provideDelegate
import java.io.File
import java.util.*
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@Suppress("DEPRECATION")
object CuiCloud {
......@@ -77,13 +80,18 @@ object CuiCloud {
val cuiCloudUrl = getUrl(project)
val key = getKey(project)
val bytes = file.readBytes()
runBlocking {
uploadToCuiCloud(
cuiCloudUrl,
key,
"/mirai/${project.name}/${file.nameWithoutExtension}.mp4",
file.readBytes()
)
retryCatching(5) {
uploadToCuiCloud(
cuiCloudUrl,
key,
"/mirai/${project.name}/${file.nameWithoutExtension}.mp4",
bytes
)
}
}
}
......@@ -129,6 +137,30 @@ object CuiCloud {
}
}
@OptIn(ExperimentalContracts::class)
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
internal inline fun <R> retryCatching(n: Int, block: () -> R): Result<R> {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
require(n >= 0) { "param n for retryCatching must not be negative" }
var exception: Throwable? = null
repeat(n) {
try {
return Result.success(block())
} catch (e: Throwable) {
try {
exception?.addSuppressed(e)
} catch (e: Throwable) {
}
exception = e
}
}
return Result.failure(exception!!)
}
inline fun <E> buildList(builderAction: MutableList<E>.() -> Unit): List<E> {
return ArrayList<E>().apply(builderAction)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment