Skip to content

Commit

Permalink
print database connection error
Browse files Browse the repository at this point in the history
  • Loading branch information
StageGuard committed Feb 9, 2021
1 parent 3d164e3 commit 240f4ff
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "KonnyakuCamp"
version = "0.3.3"
version = "0.3.5"

repositories {
mavenLocal()
Expand Down
27 changes: 15 additions & 12 deletions src/main/kotlin/stageguard/sctimetable/PluginMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.event.GlobalEventChannel
import net.mamoe.mirai.event.events.BotOnlineEvent
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.error
import net.mamoe.mirai.utils.info
import org.quartz.Scheduler
import org.quartz.impl.StdSchedulerFactory
Expand All @@ -24,7 +25,7 @@ import stageguard.sctimetable.service.*
object PluginMain : KotlinPlugin(
JvmPluginDescription(
id = "stageguard.sctimetable",
version = "0.3.3",
version = "0.3.5",
name = "SuperCourseTimetable"
)
) {
Expand All @@ -40,17 +41,19 @@ object PluginMain : KotlinPlugin(

Database.connect()

logger.info { "Waiting target Bot ${PluginConfig.qq} goes online..." }

GlobalEventChannel.filter {
it is BotOnlineEvent && it.bot.id == PluginConfig.qq
}.subscribeOnce<BotOnlineEvent> {
targetBotInstance = this.bot
TimeProviderService.start()
ScheduleListenerService.start()
RequestHandlerService.start()
BotEventRouteService.start()
}
if (Database.isConnected()) {
logger.info { "Waiting target Bot ${PluginConfig.qq} goes online..." }

GlobalEventChannel.filter {
it is BotOnlineEvent && it.bot.id == PluginConfig.qq
}.subscribeOnce<BotOnlineEvent> {
targetBotInstance = this.bot
TimeProviderService.start()
ScheduleListenerService.start()
RequestHandlerService.start()
BotEventRouteService.start()
}
} else PluginMain.logger.error("Database is not connected, services will not start.")
}

override fun onDisable() {
Expand Down
19 changes: 6 additions & 13 deletions src/main/kotlin/stageguard/sctimetable/database/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,14 @@ object Database {
} else newSuspendedTransaction(context = Dispatchers.IO, db = db) { block(this) }

fun connect() {
try {
db = Database.connect(hikariDataSourceProvider())
connectionStatus = ConnectionStatus.CONNECTED
PluginMain.logger.info { "Database ${PluginConfig.database.table} is connected." }
initDatabase()
} catch (ex: Exception) {
when(ex) {
//当配置文件的配置不符合要求时throw
is InvalidDatabaseConfigException -> {
throw ex
}
}
}
db = Database.connect(hikariDataSourceProvider())
connectionStatus = ConnectionStatus.CONNECTED
PluginMain.logger.info { "Database ${PluginConfig.database.table} is connected." }
initDatabase()
}

fun isConnected() = connectionStatus == ConnectionStatus.CONNECTED

private fun initDatabase() { query {
it.addLogger(object : SqlLogger {
override fun log(context: StatementContext, transaction: Transaction) {
Expand Down
11 changes: 10 additions & 1 deletion src/test/kotlin/RunMirai.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* Copyright 2020-2021 KonnyakuCamp.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/KonnyakuCamp/SuperCourseTimetableBot/blob/main/LICENSE
*/
package stageguard.sctimetable

import net.mamoe.mirai.alsoLogin
Expand All @@ -14,9 +22,10 @@ suspend fun main() {
PluginMain.load()
PluginMain.enable()

MiraiConsole.addBot(123123123, "123123123") {
MiraiConsole.addBot(202746796, "xZg041O\$root&.") {
fileBasedDeviceInfo()
}.alsoLogin()

MiraiConsole.job.join()

}
Expand Down

0 comments on commit 240f4ff

Please sign in to comment.