mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2024-12-22 05:57:11 +00:00
commit
1b3c479766
@ -1,5 +1,10 @@
|
||||
# Changelog
|
||||
|
||||
## 2.3.0
|
||||
|
||||
* `Bot`:
|
||||
* Add option `reconnectOptions` in database config
|
||||
|
||||
## 2.2.0
|
||||
|
||||
* `Versions`:
|
||||
|
@ -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
|
||||
)
|
@ -1,11 +1,15 @@
|
||||
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
|
||||
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 +20,24 @@ 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 reconnectOptions: DBConnectOptions? = DBConnectOptions()
|
||||
) {
|
||||
@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 = (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
|
||||
it.connector().close()
|
||||
}
|
||||
}.onFailure {
|
||||
logger.e(it)
|
||||
Thread.sleep(reconnectOptions ?.delay ?: return@onFailure)
|
||||
}.getOrNull()
|
||||
} ?: error("Unable to create database")
|
||||
}
|
||||
|
@ -5,4 +5,4 @@ kotlin.js.generate.externals=true
|
||||
kotlin.incremental=true
|
||||
|
||||
group=dev.inmo
|
||||
version=2.2.0
|
||||
version=2.3.0
|
||||
|
@ -4,7 +4,10 @@
|
||||
"driver": "org.sqlite.JDBC",
|
||||
"username": "OPTIONAL username",
|
||||
"password": "OPTIONAL password",
|
||||
"initAutomatically": false
|
||||
"reconnectOptions": {
|
||||
"attempts": 3,
|
||||
"delay": 1000
|
||||
}
|
||||
},
|
||||
"botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678",
|
||||
"plugins": [
|
||||
|
Loading…
Reference in New Issue
Block a user