diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2ab223c051e303877ce167ad9d09596f159b977f..09aff5c60a1cbd67179663d5a70b5bc7208169fd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -45,6 +45,7 @@ appdirs = "1.2.0" kstore = "0.9.1" kmpIo = "0.1.5" kotlinSemver = "2.0.0" +temerity = "[0.1.0-dev0z+41180a5,0.1.0]" kotlinx-dataframe = "0.13.1" klaxon = "5.6" @@ -98,6 +99,7 @@ kstore = { module = "io.github.xxfast:kstore", version.ref = "kstore" } kstore-file = { module = "io.github.xxfast:kstore-file", version.ref = "kstore" } kmpIo = { module = "io.github.skolson:kmp-io", version.ref = "kmpIo" } kotlinSemver = { module = "io.github.z4kn4fein:semver", version.ref = "kotlinSemver" } +temerity = { module = "edu.ucsc.its:temerity", version.ref = "temerity" } sandwich = { module = "com.github.skydoves:sandwich", version.ref = "sandwich" } sandwich-ktor = { module = "com.github.skydoves:sandwich-ktor", version.ref = "sandwich" } diff --git a/settings.gradle.kts b/settings.gradle.kts index 51e1835fae0d4507b3624fc2cc6601091c23176b..d9a15e5af808390c423889b46d8a9cd7a95ecf1d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -25,6 +25,7 @@ dependencyResolutionManagement { maven("https://maven.pkg.jetbrains.space/public/p/ktor/eap") maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev") maven("https://plugins.gradle.org/m2") + maven("https://git.ucsc.edu/api/v4/projects/12162/packages/maven") } } diff --git a/shared/shared/build.gradle.kts b/shared/shared/build.gradle.kts index c74b195f04e69d495e193739b1d539175ac8af11..75bd6ae91b60ee86f6d024c73e43c0d6e59c05e8 100644 --- a/shared/shared/build.gradle.kts +++ b/shared/shared/build.gradle.kts @@ -54,7 +54,7 @@ kotlin { sourceSets { val commonMain by getting { dependencies { - api(project(":temerity")) + api(libs.temerity) api(libs.koin.core) implementation(libs.koin.composeVm) diff --git a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/Util.kt b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/Util.kt index d14b8880bac8848e840bd382a0f549224fa55a93..3dd3c1d4cd82927de9110b62f03903842ab60b86 100644 --- a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/Util.kt +++ b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/Util.kt @@ -17,7 +17,6 @@ */ package edu.ucsc.its.temerity -import edu.ucsc.its.temerity.core.Temerity import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob @@ -37,7 +36,7 @@ internal fun currentDate(): LocalDate = Clock.System.todayIn(currentTz()) internal fun currentTime(): LocalTime = thisInstant().toLocalDateTime(currentTz()).time -internal fun Temerity.createJobScope(coroutineContext: CoroutineContext = clientCoroutineScope, allowIndependentFailure: Boolean = false): CoroutineScope { +internal fun createJobScope(coroutineContext: CoroutineContext, allowIndependentFailure: Boolean = false): CoroutineScope { val parentJob = if (allowIndependentFailure) SupervisorJob() else Job() return CoroutineScope(coroutineContext + parentJob) } diff --git a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/DispatcherFactory.kt b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/DispatcherFactory.kt index 1d775c60a7364db210b3a3844d0ae7eb4d5c4926..088ae9da8cf2b91fa5ca004c3be9cf6c40c251d2 100644 --- a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/DispatcherFactory.kt +++ b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/DispatcherFactory.kt @@ -18,13 +18,13 @@ package edu.ucsc.its.temerity.core import edu.ucsc.its.temerity.core.Temerity.Companion.DEFAULT_MINIMUM_THREAD_COUNT +import edu.ucsc.its.temerity.createJobScope +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.SupervisorJob -import kotlin.coroutines.CoroutineContext internal object DispatcherFactory { - internal fun createDispatcher(threadCount: Int): CoroutineContext = SupervisorJob() + Dispatchers.IO.limitedParallelism(threadCount) + internal fun createLibraryScope(threadCount: Int): CoroutineScope = createJobScope(Dispatchers.IO.limitedParallelism(threadCount), allowIndependentFailure = true) private fun availableThreads() = Runtime.getRuntime().availableProcessors().minus(1) diff --git a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt index 60fec02979202e2fccfc13dceb168b893e7a11c5..e3647ab3d541946f77285d55431b5ead0b9086b9 100644 --- a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt +++ b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt @@ -52,7 +52,7 @@ import edu.ucsc.its.temerity.sortByCreationDate import io.ktor.client.statement.HttpResponse import io.ktor.util.cio.toByteArray import io.ktor.utils.io.ByteReadChannel -import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.delay @@ -67,7 +67,6 @@ import org.koin.core.KoinApplication import org.koin.core.context.GlobalContext.get import org.koin.core.parameter.parametersOf import org.koin.dsl.koinApplication -import kotlin.coroutines.CoroutineContext import kotlin.time.Duration import kotlin.time.Duration.Companion.minutes import co.touchlab.kermit.Logger as KermitLogger @@ -100,7 +99,7 @@ public class Temerity internal constructor( internal val koin: Koin private var platformApi: PlatformApi private var cachedUserRoleList: ConcurrentMutableMap<Int, String> - internal var clientCoroutineScope: CoroutineContext + private var clientCoroutineScope: CoroutineScope init { check(!config.serviceToken.isNullOrBlank()) { @@ -119,7 +118,7 @@ public class Temerity internal constructor( ) } koin = temerityKoinApp.koin - clientCoroutineScope = koin.get<CoroutineContext> { + clientCoroutineScope = koin.get<CoroutineScope> { when (val optThreadCount = config.threadCount) { null -> parametersOf(DispatcherFactory.calculateMaxThreads(DEFAULT_MINIMUM_THREAD_COUNT)) else -> parametersOf(DispatcherFactory.setThreadCount(optThreadCount)) @@ -315,7 +314,7 @@ public class Temerity internal constructor( ): List<AuditLogEntry> { // TODO: Error out if user inputs time window > 1 month, since the platform API doesn't support val returnedEntries = ArrayList<AuditLogEntry>() - val jobScope = createJobScope(Dispatchers.IO) + val jobScope = createJobScope(clientCoroutineScope.coroutineContext) val requestJobs = with(eventTypeList ?: EventType.entries) { map { eventType -> jobScope.async { @@ -442,10 +441,10 @@ public class Temerity internal constructor( /** * Create a Temerity instance using Kotlin-DSL. - * Adapted from the tmdb-api project: https://github.com/MoviebaseApp/tmdb-kotlin/raw/refs/heads/main/tmdb-api/src/commonMain/kotlin/app/moviebase/tmdb/Tmdb4.kt */ @TemDsl public fun Temerity(block: TemClientConfig.() -> Unit): Temerity { + // Adapted from the tmdb-api project: https://github.com/MoviebaseApp/tmdb-kotlin/raw/refs/heads/main/tmdb-api/src/commonMain/kotlin/app/moviebase/tmdb/Tmdb4.kt val config = TemClientConfig().apply(block) return Temerity(config) } diff --git a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/di/Koin.kt b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/di/Koin.kt index 8d053379682b8c0d2f03eba0dc3b3c9ca5cf595c..3a6cc6c9d3ebaed2576061d324a128eecfa69a33 100644 --- a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/di/Koin.kt +++ b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/di/Koin.kt @@ -22,7 +22,7 @@ import de.jensklingenberg.ktorfit.ktorfit import edu.ucsc.its.temerity.TemClientConfig import edu.ucsc.its.temerity.api.PlatformApi import edu.ucsc.its.temerity.api.createPlatformApi -import edu.ucsc.its.temerity.core.DispatcherFactory.createDispatcher +import edu.ucsc.its.temerity.core.DispatcherFactory.createLibraryScope import edu.ucsc.its.temerity.core.HttpClientFactory.buildHttpClient import edu.ucsc.its.temerity.core.LoggerFactory.createLogger import io.ktor.client.HttpClient @@ -56,6 +56,6 @@ internal val libModule = module { ktorfit.createPlatformApi() } single { (threadCount: Int) -> - createDispatcher(threadCount) + createLibraryScope(threadCount) } }