feat(launch): 延后 BotConfig 的反序列化时机.

通过将 BotConfig 的反序列化时机延后到启动机器人的时候, 可以避免因某个机器人配置错误导致所有机器人都无法启动的问题.
注意, 语法错误还是会在启动时报错, 只是说部分序列化器会检查字段值是否有误, 通过延后反序列化来防止全反序列化的时候一个配置炸了影响全部而已.
This commit is contained in:
2022-07-04 16:40:17 +08:00
parent 3c54c33364
commit 51d036c4c6
2 changed files with 30 additions and 12 deletions

View File

@ -1,5 +1,6 @@
package net.lamgc.scalabot
import com.google.gson.JsonParseException
import io.prometheus.client.exporter.HTTPServer
import kotlinx.coroutines.runBlocking
import mu.KotlinLogging
@ -110,22 +111,39 @@ internal class Launcher(private val config: AppConfig = Const.config) : AutoClos
@Synchronized
fun launch(): Boolean {
val botConfigs = loadBotConfig() ?: return false
if (botConfigs.isEmpty()) {
val botConfigs = loadBotConfigJson() ?: return false
if (botConfigs.isEmpty) {
log.warn { "尚未配置任何机器人, 请先配置机器人后再启动本程序." }
return false
} else if (botConfigs.none { it.enabled }) {
log.warn { "配置文件中没有已启用的机器人, 请至少启用一个机器人." }
return false
}
for (botConfig in botConfigs) {
var launchedCounts = 0
for (botConfigJson in botConfigs) {
val botConfig = try {
GsonConst.botConfigGson.fromJson(botConfigJson, BotConfig::class.java)
} catch (e: JsonParseException) {
val botName = try {
botConfigJson.asJsonObject.get("account")?.asJsonObject?.get("name")?.asString ?: "Unknown"
} catch (e: Exception) {
"Unknown"
}
log.error(e) { "机器人 `$botName` 配置有误, 跳过该机器人的启动." }
continue
}
try {
launchBot(botConfig)
launchedCounts++
} catch (e: Exception) {
log.error(e) { "机器人 `${botConfig.account.name}` 启动时发生错误." }
}
}
return true
return if (launchedCounts != 0) {
log.info { "已启动 $launchedCounts 个机器人." }
true
} else {
log.warn { "未启动任何机器人, 请检查配置并至少启用一个机器人." }
false
}
}
private fun launchBot(botConfig: BotConfig) {