Skip to content

Commit 240f4ff

Browse files
committed
print database connection error
1 parent 3d164e3 commit 240f4ff

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "KonnyakuCamp"
9-
version = "0.3.3"
9+
version = "0.3.5"
1010

1111
repositories {
1212
mavenLocal()

src/main/kotlin/stageguard/sctimetable/PluginMain.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
1414
import net.mamoe.mirai.event.GlobalEventChannel
1515
import net.mamoe.mirai.event.events.BotOnlineEvent
1616
import net.mamoe.mirai.utils.MiraiExperimentalApi
17+
import net.mamoe.mirai.utils.error
1718
import net.mamoe.mirai.utils.info
1819
import org.quartz.Scheduler
1920
import org.quartz.impl.StdSchedulerFactory
@@ -24,7 +25,7 @@ import stageguard.sctimetable.service.*
2425
object PluginMain : KotlinPlugin(
2526
JvmPluginDescription(
2627
id = "stageguard.sctimetable",
27-
version = "0.3.3",
28+
version = "0.3.5",
2829
name = "SuperCourseTimetable"
2930
)
3031
) {
@@ -40,17 +41,19 @@ object PluginMain : KotlinPlugin(
4041

4142
Database.connect()
4243

43-
logger.info { "Waiting target Bot ${PluginConfig.qq} goes online..." }
44-
45-
GlobalEventChannel.filter {
46-
it is BotOnlineEvent && it.bot.id == PluginConfig.qq
47-
}.subscribeOnce<BotOnlineEvent> {
48-
targetBotInstance = this.bot
49-
TimeProviderService.start()
50-
ScheduleListenerService.start()
51-
RequestHandlerService.start()
52-
BotEventRouteService.start()
53-
}
44+
if (Database.isConnected()) {
45+
logger.info { "Waiting target Bot ${PluginConfig.qq} goes online..." }
46+
47+
GlobalEventChannel.filter {
48+
it is BotOnlineEvent && it.bot.id == PluginConfig.qq
49+
}.subscribeOnce<BotOnlineEvent> {
50+
targetBotInstance = this.bot
51+
TimeProviderService.start()
52+
ScheduleListenerService.start()
53+
RequestHandlerService.start()
54+
BotEventRouteService.start()
55+
}
56+
} else PluginMain.logger.error("Database is not connected, services will not start.")
5457
}
5558

5659
override fun onDisable() {

src/main/kotlin/stageguard/sctimetable/database/Database.kt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,14 @@ object Database {
4747
} else newSuspendedTransaction(context = Dispatchers.IO, db = db) { block(this) }
4848

4949
fun connect() {
50-
try {
51-
db = Database.connect(hikariDataSourceProvider())
52-
connectionStatus = ConnectionStatus.CONNECTED
53-
PluginMain.logger.info { "Database ${PluginConfig.database.table} is connected." }
54-
initDatabase()
55-
} catch (ex: Exception) {
56-
when(ex) {
57-
//当配置文件的配置不符合要求时throw
58-
is InvalidDatabaseConfigException -> {
59-
throw ex
60-
}
61-
}
62-
}
50+
db = Database.connect(hikariDataSourceProvider())
51+
connectionStatus = ConnectionStatus.CONNECTED
52+
PluginMain.logger.info { "Database ${PluginConfig.database.table} is connected." }
53+
initDatabase()
6354
}
6455

56+
fun isConnected() = connectionStatus == ConnectionStatus.CONNECTED
57+
6558
private fun initDatabase() { query {
6659
it.addLogger(object : SqlLogger {
6760
override fun log(context: StatementContext, transaction: Transaction) {

src/test/kotlin/RunMirai.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*
2+
* Copyright 2020-2021 KonnyakuCamp.
3+
*
4+
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
5+
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
6+
*
7+
* https://github.com/KonnyakuCamp/SuperCourseTimetableBot/blob/main/LICENSE
8+
*/
19
package stageguard.sctimetable
210

311
import net.mamoe.mirai.alsoLogin
@@ -14,9 +22,10 @@ suspend fun main() {
1422
PluginMain.load()
1523
PluginMain.enable()
1624

17-
MiraiConsole.addBot(123123123, "123123123") {
25+
MiraiConsole.addBot(202746796, "xZg041O\$root&.") {
1826
fileBasedDeviceInfo()
1927
}.alsoLogin()
28+
2029
MiraiConsole.job.join()
2130

2231
}

0 commit comments

Comments
 (0)