From b17931e7bd3ac74bc54f46d995cf30c6b4d6edbf Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 5 Dec 2022 22:31:15 +0600 Subject: [PATCH] complete startup module --- startup/launcher/build.gradle | 1 - .../src/commonMain/kotlin/ServerLauncher.kt | 34 ----------- .../src/commonMain/kotlin/StartupLauncher.kt | 57 +++++++++++++++++++ .../src/jvmMain/kotlin/ServerLauncher.kt | 36 ++++++------ startup/plugin/build.gradle | 2 + .../src/commonMain/kotlin/ServerPlugin.kt | 2 +- 6 files changed, 80 insertions(+), 52 deletions(-) delete mode 100644 startup/launcher/src/commonMain/kotlin/ServerLauncher.kt create mode 100644 startup/launcher/src/commonMain/kotlin/StartupLauncher.kt diff --git a/startup/launcher/build.gradle b/startup/launcher/build.gradle index 5b3c3a61734..5e762e51950 100644 --- a/startup/launcher/build.gradle +++ b/startup/launcher/build.gradle @@ -10,7 +10,6 @@ kotlin { commonMain { dependencies { api internalProject("micro_utils.startup.plugin") - api libs.kslog } } } diff --git a/startup/launcher/src/commonMain/kotlin/ServerLauncher.kt b/startup/launcher/src/commonMain/kotlin/ServerLauncher.kt deleted file mode 100644 index a7f871d261a..00000000000 --- a/startup/launcher/src/commonMain/kotlin/ServerLauncher.kt +++ /dev/null @@ -1,34 +0,0 @@ -package dev.inmo.micro_utils.startup.launcher - -import dev.inmo.micro_utils.startup.plugin.ServerPlugin -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonObject -import org.koin.core.Koin -import org.koin.core.module.Module -import org.koin.dsl.module - -object ServerLauncher : ServerPlugin { - override fun Module.setupDI(config: JsonObject) { - val pluginsConfig = defaultJson.decodeFromJsonElement(Config.serializer(), config) - - single { pluginsConfig } - - includes( - pluginsConfig.plugins.map { - module { - with(it) { - setupDI(config) - } - } - } - ) - } - - override suspend fun Koin.startPlugin() { - get().plugins.forEach { - with(it) { - startPlugin() - } - } - } -} diff --git a/startup/launcher/src/commonMain/kotlin/StartupLauncher.kt b/startup/launcher/src/commonMain/kotlin/StartupLauncher.kt new file mode 100644 index 00000000000..9640df8f299 --- /dev/null +++ b/startup/launcher/src/commonMain/kotlin/StartupLauncher.kt @@ -0,0 +1,57 @@ +package dev.inmo.micro_utils.startup.launcher + +import dev.inmo.kslog.common.i +import dev.inmo.kslog.common.taggedLogger +import dev.inmo.kslog.common.w +import dev.inmo.micro_utils.coroutines.runCatchingSafely +import dev.inmo.micro_utils.startup.plugin.ServerPlugin +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.joinAll +import kotlinx.coroutines.launch +import kotlinx.serialization.json.JsonObject +import org.koin.core.Koin +import org.koin.core.module.Module +import org.koin.dsl.module + +object StartupLauncher : ServerPlugin { + internal val logger = taggedLogger(this) + override fun Module.setupDI(config: JsonObject) { + val pluginsConfig = defaultJson.decodeFromJsonElement(Config.serializer(), config) + + single { pluginsConfig } + single { CoroutineScope(Dispatchers.Default) } + + includes( + pluginsConfig.plugins.mapNotNull { + runCatching { + module { + with(it) { + setupDI(config) + } + } + }.onFailure { e -> + logger.w("Unable to load DI part of $it", e) + }.getOrNull() + } + ) + } + + override suspend fun startPlugin(koin: Koin) { + val scope = koin.get() + koin.get().plugins.map { plugin -> + scope.launch { + runCatchingSafely { + logger.i("Start loading of $plugin") + with(plugin) { + startPlugin(koin) + } + }.onFailure { e -> + logger.w("Unable to load bot part of $plugin", e) + }.onSuccess { + logger.i("Complete loading of $plugin") + } + } + }.joinAll() + } +} diff --git a/startup/launcher/src/jvmMain/kotlin/ServerLauncher.kt b/startup/launcher/src/jvmMain/kotlin/ServerLauncher.kt index 690e1813227..2a72b7707af 100644 --- a/startup/launcher/src/jvmMain/kotlin/ServerLauncher.kt +++ b/startup/launcher/src/jvmMain/kotlin/ServerLauncher.kt @@ -2,31 +2,35 @@ package dev.inmo.micro_utils.startup.launcher import dev.inmo.kslog.common.KSLog import dev.inmo.kslog.common.i -import dev.inmo.micro_utils.startup.plugin.ServerPlugin -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonObject -import org.koin.core.Koin -import org.koin.core.module.Module +import dev.inmo.micro_utils.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.serialization.json.jsonObject +import org.koin.core.KoinApplication +import org.koin.core.context.GlobalContext import org.koin.dsl.module import java.io.File -package dev.inmo.micro_utils.startup.launcher - -import dev.inmo. - -class ServerLauncher : ServerPlugin { - val defaultJson = Json { - ignoreUnknownKeys = true - } - suspend fun main(args: Array) { - KSLog.default = KSLog("PlaguBot") + KSLog.default = KSLog("ServerLauncher") val (configPath) = args val file = File(configPath) KSLog.i("Start read config from ${file.absolutePath}") val json = defaultJson.parseToJsonElement(file.readText()).jsonObject KSLog.i("Config has been read") - ServerLauncher.start().join() + with(StartupLauncher) { + logger.i("Start initialization") + val koinApp = KoinApplication.init() + koinApp.modules( + module { + setupDI(json) + } + ) + logger.i("Modules loaded") + GlobalContext.startKoin(koinApp) + logger.i("Koin started") + startPlugin(koinApp.koin) + logger.i("Behaviour builder has been setup") + } } diff --git a/startup/plugin/build.gradle b/startup/plugin/build.gradle index f7e590a535b..851613c225e 100644 --- a/startup/plugin/build.gradle +++ b/startup/plugin/build.gradle @@ -11,6 +11,8 @@ kotlin { dependencies { api libs.koin api libs.kt.serialization + api libs.kslog + api project(":micro_utils.coroutines") } } } diff --git a/startup/plugin/src/commonMain/kotlin/ServerPlugin.kt b/startup/plugin/src/commonMain/kotlin/ServerPlugin.kt index 91cfc36f0b5..25019809ad5 100644 --- a/startup/plugin/src/commonMain/kotlin/ServerPlugin.kt +++ b/startup/plugin/src/commonMain/kotlin/ServerPlugin.kt @@ -14,7 +14,7 @@ import org.koin.core.module.Module interface ServerPlugin { fun Module.setupDI(config: JsonObject) {} - suspend fun Koin.startPlugin() {} + suspend fun startPlugin(koin: Koin) {} companion object : KSerializer { override val descriptor: SerialDescriptor