From 004df7d7f99880b46a5a96ecbebd12d827ce2301 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Tue, 19 Nov 2024 15:26:10 +0800 Subject: [PATCH] feat: add scheduler --- .../src/main/kotlin/cn/rtast/rob/qqbot/BotInstance.kt | 7 +++++++ .../src/main/kotlin/cn/rtast/rob/qqbot/QBotFactory.kt | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/ronebot-qqbot-webhook/src/main/kotlin/cn/rtast/rob/qqbot/BotInstance.kt b/ronebot-qqbot-webhook/src/main/kotlin/cn/rtast/rob/qqbot/BotInstance.kt index a676751..4f086d9 100644 --- a/ronebot-qqbot-webhook/src/main/kotlin/cn/rtast/rob/qqbot/BotInstance.kt +++ b/ronebot-qqbot-webhook/src/main/kotlin/cn/rtast/rob/qqbot/BotInstance.kt @@ -4,6 +4,7 @@ * Date: 2024/11/15 */ +@file:Suppress("unused") package cn.rtast.rob.qqbot @@ -11,6 +12,7 @@ import cn.rtast.rob.BaseBotInstance import cn.rtast.rob.qqbot.qbot.QQBotAction import cn.rtast.rob.qqbot.qbot.QQBotListener import cn.rtast.rob.qqbot.util.HttpServer +import cn.rtast.rob.scheduler.BotCoroutineScheduler import io.ktor.server.engine.* import io.ktor.server.netty.* @@ -25,6 +27,11 @@ class BotInstance internal constructor( internal val action = QQBotAction(appId, clientSecret, this) internal lateinit var httpServer: EmbeddedServer + /** + * Bot实例作用于的任务调度器 + */ + val scheduler = BotCoroutineScheduler(this) + override suspend fun createBot(): BotInstance { httpServer = HttpServer(port, appId, clientSecret, listener, this).startHttpServer() return this diff --git a/ronebot-qqbot-webhook/src/main/kotlin/cn/rtast/rob/qqbot/QBotFactory.kt b/ronebot-qqbot-webhook/src/main/kotlin/cn/rtast/rob/qqbot/QBotFactory.kt index 2fcf332..a647234 100644 --- a/ronebot-qqbot-webhook/src/main/kotlin/cn/rtast/rob/qqbot/QBotFactory.kt +++ b/ronebot-qqbot-webhook/src/main/kotlin/cn/rtast/rob/qqbot/QBotFactory.kt @@ -4,16 +4,23 @@ * Date: 2024/11/11 */ +@file:Suppress("unused") package cn.rtast.rob.qqbot import cn.rtast.rob.BotFactory import cn.rtast.rob.qqbot.qbot.QQBotListener +import cn.rtast.rob.scheduler.GlobalCoroutineScheduler object QBotFactory : BotFactory { internal val botInstances = mutableListOf() + /** + * 全局作用域的任务调度器 + */ + val globalScheduler = GlobalCoroutineScheduler(botInstances) + suspend fun createServer(port: Int, appId: String, clientSecret: String, listener: QQBotListener): BotInstance { val instance = BotInstance(port, appId, clientSecret, listener).apply { createBot() } botInstances.add(instance)