MicroUtils/startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt

165 lines
6.3 KiB
Kotlin
Raw Normal View History

@file:GenerateKoinDefinition("baseJsonProvider", Json::class)
2022-12-05 16:31:15 +00:00
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.koin.annotations.GenerateKoinDefinition
2022-12-25 04:26:44 +00:00
import dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin.setupDI
import dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin.startPlugin
2022-12-06 07:06:21 +00:00
import dev.inmo.micro_utils.startup.plugin.StartPlugin
2022-12-05 16:31:15 +00:00
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
2022-12-06 07:06:21 +00:00
import kotlinx.serialization.SerialFormat
import kotlinx.serialization.StringFormat
import kotlinx.serialization.json.Json
2022-12-05 16:31:15 +00:00
import kotlinx.serialization.json.JsonObject
2022-12-25 04:26:44 +00:00
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.modules.SerializersModule
2022-12-05 16:31:15 +00:00
import org.koin.core.Koin
2022-12-25 04:26:44 +00:00
import org.koin.core.KoinApplication
import org.koin.core.context.startKoin
2022-12-05 16:31:15 +00:00
import org.koin.core.module.Module
2022-12-06 07:06:21 +00:00
import org.koin.dsl.binds
2022-12-05 16:31:15 +00:00
import org.koin.dsl.module
2022-12-06 07:06:21 +00:00
/**
* Default startup plugin. See [setupDI] and [startPlugin] for more info
*/
object StartLauncherPlugin : StartPlugin {
2022-12-05 16:31:15 +00:00
internal val logger = taggedLogger(this)
2022-12-06 07:06:21 +00:00
2022-12-25 04:26:44 +00:00
fun Module.setupDI(config: Config, rawJsonObject: JsonObject? = null) {
val rawJsonObject = rawJsonObject ?: defaultJson.encodeToJsonElement(Config.serializer(), config).jsonObject
2022-12-05 16:31:15 +00:00
2022-12-25 04:26:44 +00:00
single { rawJsonObject }
single { config }
2022-12-05 16:31:15 +00:00
single { CoroutineScope(Dispatchers.Default) }
single {
val serializersModules = getAll<SerializersModule>().distinct()
val baseJson = baseJsonProvider ?: defaultJson
if (serializersModules.isEmpty()) {
baseJson
} else {
Json(baseJson) {
2023-05-09 09:09:37 +00:00
serializersModule = SerializersModule {
include(baseJson.serializersModule)
serializersModules.forEach { include(it) }
}
}
}
} binds arrayOf(StringFormat::class, SerialFormat::class)
2022-12-05 16:31:15 +00:00
includes(
2022-12-25 04:26:44 +00:00
config.plugins.mapNotNull {
2023-02-22 04:28:25 +00:00
val pluginName = it::class.simpleName ?: it.toString()
2022-12-05 16:31:15 +00:00
runCatching {
2023-02-22 04:28:25 +00:00
logger.i { "Start koin module registration for $pluginName" }
2022-12-05 16:31:15 +00:00
module {
with(it) {
2022-12-25 04:26:44 +00:00
setupDI(rawJsonObject)
2022-12-05 16:31:15 +00:00
}
}
}.onFailure { e ->
2023-02-22 04:28:25 +00:00
logger.w("Unable to register koin module of $pluginName", e)
}.onSuccess {
logger.i("Successfully registered koin module of $pluginName")
2022-12-05 16:31:15 +00:00
}.getOrNull()
}
)
}
2022-12-25 04:26:44 +00:00
/**
* Will deserialize [Config] from [config], register it in receiver [Module] (as well as [CoroutineScope] and
* [kotlinx.serialization.json.Json])
*
* Besides, in this method will be called [StartPlugin.setupDI] on each plugin from [Config.plugins]. In case when
* some plugin will not be loaded correctly it will be reported throw the [logger]
*/
override fun Module.setupDI(config: JsonObject) {
logger.i("Koin for current module has started setup")
setupDI(
defaultJson.decodeFromJsonElement(Config.serializer(), config),
config
)
logger.i("Koin for current module has been setup")
}
2022-12-06 07:06:21 +00:00
/**
* Takes [CoroutineScope] and [Config] from the [koin], and call starting of each plugin from [Config.plugins]
* ASYNCHRONOUSLY. Just like in [setupDI], in case of fail in some plugin it will be reported using [logger]
*/
2022-12-05 16:31:15 +00:00
override suspend fun startPlugin(koin: Koin) {
2022-12-25 04:26:44 +00:00
logger.i("Start starting of subplugins")
2022-12-05 16:31:15 +00:00
val scope = koin.get<CoroutineScope>()
koin.get<Config>().plugins.map { plugin ->
2023-02-22 04:28:25 +00:00
val pluginName = plugin::class.simpleName ?: plugin.toString()
2022-12-05 16:31:15 +00:00
scope.launch {
runCatchingSafely {
2023-02-22 04:28:25 +00:00
logger.i("Start loading of $pluginName")
2022-12-05 16:31:15 +00:00
with(plugin) {
startPlugin(koin)
}
}.onFailure { e ->
2023-02-22 04:28:25 +00:00
logger.w("Unable to start plugin $pluginName", e)
2022-12-05 16:31:15 +00:00
}.onSuccess {
2023-02-22 04:28:25 +00:00
logger.i("Complete loading of $pluginName")
2022-12-05 16:31:15 +00:00
}
}
}.joinAll()
2022-12-25 04:26:44 +00:00
logger.i("Complete subplugins start")
}
/**
* Will create [KoinApplication], init, load modules using [StartLauncherPlugin] and start plugins using the same base
* plugin. It is basic [start] method which accepts both [config] and [rawConfig] which suppose to be the same or
* at least [rawConfig] must contain serialized variant of [config]
2022-12-25 04:26:44 +00:00
*
* @param rawConfig It is expected that this [JsonObject] will contain serialized [Config] ([StartLauncherPlugin] will
* deserialize it in its [StartLauncherPlugin.setupDI]
*/
suspend fun start(config: Config, rawConfig: JsonObject) {
2022-12-25 04:26:44 +00:00
logger.i("Start initialization")
val koinApp = KoinApplication.init()
koinApp.modules(
module {
setupDI(config, rawConfig)
2022-12-25 04:26:44 +00:00
}
)
logger.i("Modules loaded")
startKoin(koinApp)
logger.i("Koin started")
startPlugin(koinApp.koin)
logger.i("App has been setup")
}
/**
* Call [start] with deserialized [Config] as config and [rawConfig] as is
*
* @param rawConfig It is expected that this [JsonObject] will contain serialized [Config]
*/
suspend fun start(rawConfig: JsonObject) {
start(defaultJson.decodeFromJsonElement(Config.serializer(), rawConfig), rawConfig)
}
/**
* Call [start] with deserialized [Config] as is and serialize it to [JsonObject] to pass as the first parameter
* to the basic [start] method
2022-12-25 04:26:44 +00:00
*
* @param config Will be converted to [JsonObject] as raw config. That means that all plugins from [config] will
* receive serialized version of [config] in [StartPlugin.setupDI] method
2022-12-25 04:26:44 +00:00
*/
suspend fun start(config: Config) {
start(config, defaultJson.encodeToJsonElement(Config.serializer(), config).jsonObject)
2022-12-25 04:26:44 +00:00
2022-12-05 16:31:15 +00:00
}
}