Skip to content

Commit

Permalink
Merge pull request #694 from simple-robot/dev/fix_sb_bot_autoregister…
Browse files Browse the repository at this point in the history
…_policy

使 Spring Boot 支持bot自动注册失败策略的配置
  • Loading branch information
ForteScarlet authored May 31, 2023
2 parents db3b69a + f60952d commit b4b39ca
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 16 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/P.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ sealed class P(override val group: String) : ProjectDetail() {
val versionWithoutSnapshot: Version

init {
val mainVersion = version(3, 1, 0)
val mainVersion = version(3, 1, 1)

fun initVersionWithoutSnapshot(status: Version?): Version = if (status == null) {
mainVersion
Expand Down
6 changes: 3 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ include(":simbot-util-suspend-transformer")
// project test
// if not in CI workflows
if (!System.getenv("IS_CI").toBoolean()) {
// include(
include(
// projectTest("boot"),
// projectTest("spring-boot-starter"),
projectTest("spring-boot-starter"),
// projectTest("jmh"),
// )
)

include(":simbot-component-http-server-api")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ package love.forte.simboot.spring.autoconfigure

import kotlinx.serialization.ExperimentalSerializationApi
import love.forte.simboot.core.application.BootApplicationConfiguration
import love.forte.simboot.core.application.BotAutoRegistrationFailureException
import love.forte.simboot.core.application.BotRegistrationFailurePolicy
import love.forte.simbot.ExperimentalSimbotApi
import love.forte.simbot.InternalSimbotApi
import love.forte.simbot.application.Application
import love.forte.simbot.application.EventProvider
import love.forte.simbot.bot.*
Expand Down Expand Up @@ -53,7 +56,7 @@ public open class SimbotSpringBootBotAutoRegisterBuildConfigure {
}


@OptIn(ExperimentalSerializationApi::class, ExperimentalSimbotApi::class)
@OptIn(ExperimentalSerializationApi::class, ExperimentalSimbotApi::class, InternalSimbotApi::class)
private suspend fun config(
application: Application,
customDecoderFactories: List<BotVerifyInfoDecoderFactory<*, *>>
Expand All @@ -64,17 +67,34 @@ public open class SimbotSpringBootBotAutoRegisterBuildConfigure {

val configuration = application.configuration as? BootApplicationConfiguration

logger.debug("Boot configuration: {}", configuration)

val policy = configuration?.botAutoRegistrationFailurePolicy ?: BotRegistrationFailurePolicy.ERROR

if (configuration == null) {
logger.debug("Bot registration Failure policy: {} (by default)", policy)
} else {
logger.debug("Bot registration Failure policy: {}", policy)
}

val botConfigResources = (configuration?.botConfigurationResources ?: emptyList())
.asSequence()
.flatMap {
try {
resolver.getResources(it).asList()
} catch (fne: FileNotFoundException) {
// 由于 FileNotFoundException 而找不到资源 A
logger.warn(
"Can not resolve resource path 「{}」: {}, just use empty resources.",
"Resource path 「{}」 could not be resolved because of FileNotFoundException(message={}), just use empty resources.",
it,
fne.localizedMessage
)
logger.debug(
"Resource path 「{}」 could not be resolved because of FileNotFoundException(message={}), just use empty resources.",
it,
fne.localizedMessage,
fne
)
emptyList()
}
}
Expand Down Expand Up @@ -112,7 +132,9 @@ public open class SimbotSpringBootBotAutoRegisterBuildConfigure {

if (botConfigResources.isNotEmpty()) {
botConfigResources.forEach { res ->
register(application.providers, res).also { bot ->
@Suppress("DuplicatedCode")
try {
val bot = register(application.providers, res)
if (bot != null) {
val isAutoStart =
(application.configuration as? BootApplicationConfiguration)?.isAutoStartBots == true
Expand All @@ -131,7 +153,7 @@ public open class SimbotSpringBootBotAutoRegisterBuildConfigure {
is BootApplicationConfiguration -> "the configuration property 'autoStartBots' is false"
else -> "the type of application configuration is not BootApplicationConfiguration"
}

logger.info(
"Bot info [{}] successfully registered as Bot(component={}, id={}]) but it does not start automatically because {}",
res,
Expand All @@ -141,13 +163,60 @@ public open class SimbotSpringBootBotAutoRegisterBuildConfigure {
)
}
} else {
logger.warn(
"Bot verify info [{}] not registered by any registrars, skip. The botRegistrar: {}",
res,
this
)
// bot is null
when (policy) {
BotRegistrationFailurePolicy.ERROR -> {
val err = BotAutoRegistrationFailureException("Bot($res)")
logger.error("Bot verify info [{}] is not matched by any manager.", res, err)
throw err
}

BotRegistrationFailurePolicy.WARN -> {
val warn = BotAutoRegistrationFailureException("Bot($res)") // For log only.
logger.warn("Bot verify info [{}] is not matched by any manager.", res, warn)
}

else -> {
// do nothing.
logger.warn(
"Bot verify info [{}] not registered by any registrars, skip. The botRegistrar: {}",
res,
this
)
}
}
}
} catch (e: Throwable) {
when (policy) {
BotRegistrationFailurePolicy.ERROR -> {
logger.error(
"Bot verify info [{}] registration failed or start failed (with {})",
res,
this
)
throw e
}

BotRegistrationFailurePolicy.IGNORE -> {
logger.debug(
"Bot verify info [{}] registration failed or start failed (with {})",
res,
this,
e
)
}

BotRegistrationFailurePolicy.WARN -> {
logger.warn(
"Bot verify info [{}] registration failed or start failed (with {})",
res,
this,
e
)
}
}
}

}
}

Expand Down Expand Up @@ -175,7 +244,6 @@ public open class SimbotSpringBootBotAutoRegisterBuildConfigure {
}
}

logger.warn("Bot verify info [{}] is not matched by any manager, skip it.", botVerifyInfo)
return null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,7 @@ private fun EventListenerRegistrationDescriptionsGenerator.autoScanTopFunction(
}


@OptIn(InternalSimbotApi::class)
private fun BotRegistrar.autoRegisterBots(
classLoader: ClassLoader,
logger: Logger,
Expand Down Expand Up @@ -1094,6 +1095,7 @@ private fun BotRegistrar.autoRegisterBots(
}

if (bot == null) {
@Suppress("DuplicatedCode")
when (failurePolicy) {
BotRegistrationFailurePolicy.ERROR -> {
val err = BotAutoRegistrationFailureException("Bot($botInfo)")
Expand All @@ -1118,7 +1120,7 @@ private fun BotRegistrar.autoRegisterBots(
* 通过自动扫描注册bot时bot无法注册时出现的异常。
*
*/
public class BotAutoRegistrationFailureException internal constructor(message: String?) : IllegalStateException(message)
public class BotAutoRegistrationFailureException @InternalSimbotApi constructor(message: String?) : IllegalStateException(message)


private val KClass<*>.allFunctions: List<KFunction<*>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

package love.forte.test.springboot

import love.forte.simboot.spring.autoconfigure.EnableSimbot
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
@EnableSimbot
open class Main

fun main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

logging.level.love.forte=DEBUG
simbot.bot-auto-registration-failure-policy=error
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"component": "simbot.test",
"code": 1234
}

0 comments on commit b4b39ca

Please sign in to comment.