Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使 Spring Boot 支持bot自动注册失败策略的配置 #694

Merged
merged 3 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}