From ab0578b31210cabfd458ce65467cae8516c3beb9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 4 Sep 2022 14:31:46 +0600 Subject: [PATCH 1/4] start 2.3.0 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ccb847..bedbdfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 2.3.0 + ## 2.2.0 * `Versions`: diff --git a/gradle.properties b/gradle.properties index d8a5fa2..943a0ed 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,4 +5,4 @@ kotlin.js.generate.externals=true kotlin.incremental=true group=dev.inmo -version=2.2.0 +version=2.3.0 From dfbc7be6f2530a0636e99bdd5da85a31c32a6462 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 4 Sep 2022 14:37:22 +0600 Subject: [PATCH 2/4] add option waitForConnection in database config --- CHANGELOG.md | 3 ++ .../inmo/plagubot/config/DatabaseConfig.kt | 32 ++++++++++++++----- template.config.json | 2 +- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bedbdfa..83614d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 2.3.0 +* `Bot`: + * Add option `waitForConnection` in database config + ## 2.2.0 * `Versions`: diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt b/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt index 7d38b13..0291200 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt @@ -6,6 +6,7 @@ import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.transactions.transactionManager import org.koin.core.scope.Scope import org.sqlite.JDBC +import java.lang.Exception import java.sql.Connection inline val Scope.database: Database? @@ -16,15 +17,30 @@ data class DatabaseConfig( val url: String = "jdbc:sqlite:file:test?mode=memory&cache=shared", val driver: String = JDBC::class.qualifiedName!!, val username: String = "", - val password: String = "" + val password: String = "", + val waitForConnection: Boolean = true ) { @Transient - val database: Database = Database.connect( - url, - driver, - username, - password - ).also { - it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE // Or Connection.TRANSACTION_READ_UNCOMMITTED + val database: Database by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { + while (true) { + return@lazy try { + Database.connect( + url, + driver, + username, + password + ).also { + it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE // Or Connection.TRANSACTION_READ_UNCOMMITTED + } + } catch (e: Throwable) { + if (waitForConnection) { + Thread.sleep(1000L) + continue + } else { + throw e + } + } + } + error("Unable to get database by some reason") } } diff --git a/template.config.json b/template.config.json index 0d12e3e..ad6d9c5 100644 --- a/template.config.json +++ b/template.config.json @@ -4,7 +4,7 @@ "driver": "org.sqlite.JDBC", "username": "OPTIONAL username", "password": "OPTIONAL password", - "initAutomatically": false + "waitForConnection": true }, "botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678", "plugins": [ From 763718716d1fc9a600cb0907f0c2a67bc067fd73 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 4 Sep 2022 15:07:30 +0600 Subject: [PATCH 3/4] improve reconnect feature --- .../inmo/plagubot/config/DBConnectOptions.kt | 9 +++++ .../inmo/plagubot/config/DatabaseConfig.kt | 40 +++++++++---------- template.config.json | 5 ++- 3 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 bot/src/main/kotlin/dev/inmo/plagubot/config/DBConnectOptions.kt diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/config/DBConnectOptions.kt b/bot/src/main/kotlin/dev/inmo/plagubot/config/DBConnectOptions.kt new file mode 100644 index 0000000..d072b3f --- /dev/null +++ b/bot/src/main/kotlin/dev/inmo/plagubot/config/DBConnectOptions.kt @@ -0,0 +1,9 @@ +package dev.inmo.plagubot.config + +import kotlinx.serialization.Serializable + +@Serializable +data class DBConnectOptions( + val attempts: Int = 3, + val delay: Long = 1000L +) diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt b/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt index 0291200..6b864b8 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt @@ -1,5 +1,8 @@ package dev.inmo.plagubot.config +import dev.inmo.kslog.common.e +import dev.inmo.kslog.common.logger +import kotlinx.coroutines.delay import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import org.jetbrains.exposed.sql.Database @@ -18,29 +21,22 @@ data class DatabaseConfig( val driver: String = JDBC::class.qualifiedName!!, val username: String = "", val password: String = "", - val waitForConnection: Boolean = true + val reconnectOptions: DBConnectOptions? = DBConnectOptions() ) { @Transient - val database: Database by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { - while (true) { - return@lazy try { - Database.connect( - url, - driver, - username, - password - ).also { - it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE // Or Connection.TRANSACTION_READ_UNCOMMITTED - } - } catch (e: Throwable) { - if (waitForConnection) { - Thread.sleep(1000L) - continue - } else { - throw e - } + val database: Database = (0 until (reconnectOptions ?.attempts ?: 1)).firstNotNullOfOrNull { + runCatching { + Database.connect( + url, + driver, + username, + password + ).also { + it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE // Or Connection.TRANSACTION_READ_UNCOMMITTED } - } - error("Unable to get database by some reason") - } + }.onFailure { + logger.e(it) + Thread.sleep(reconnectOptions ?.delay ?: return@onFailure) + }.getOrNull() + } ?: error("Unable to create database") } diff --git a/template.config.json b/template.config.json index ad6d9c5..4d7245f 100644 --- a/template.config.json +++ b/template.config.json @@ -4,7 +4,10 @@ "driver": "org.sqlite.JDBC", "username": "OPTIONAL username", "password": "OPTIONAL password", - "waitForConnection": true + "reconnectOptions": { + "attempts": 3, + "delay": 1000 + } }, "botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678", "plugins": [ From dcfb66ef6ce9d5f7222a2c6718287c06f4bab789 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 4 Sep 2022 15:57:40 +0600 Subject: [PATCH 4/4] fixes --- CHANGELOG.md | 2 +- bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83614d6..f9333c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## 2.3.0 * `Bot`: - * Add option `waitForConnection` in database config + * Add option `reconnectOptions` in database config ## 2.2.0 diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt b/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt index 6b864b8..5a8cd0e 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt @@ -33,6 +33,7 @@ data class DatabaseConfig( password ).also { it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE // Or Connection.TRANSACTION_READ_UNCOMMITTED + it.connector().close() } }.onFailure { logger.e(it)