Commit c9930be9 authored by Jose V. Trigueros's avatar Jose V. Trigueros

Threads!

parent 2766bdcc
Pipeline #137485673 passed with stages
in 11 minutes and 21 seconds
package tech.gdragon.commands.debug
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent
import org.joda.time.DateTime
import tech.gdragon.commands.CommandHandler
import tech.gdragon.db.asyncTransaction
import tech.gdragon.db.dao.Guild
class Async : CommandHandler() {
override fun action(args: Array<String>, event: GuildMessageReceivedEvent) {
for (i in 1..1000) {
asyncTransaction {
val guild = Guild[333055724198559745L]
guild.lastActiveOn = DateTime.now()
print("$i,")
}
}
}
override fun usage(prefix: String): String {
TODO("Not yet implemented")
}
override fun description(): String {
TODO("Not yet implemented")
}
}
......@@ -7,23 +7,25 @@ import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.transactions.transactionManager
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.Future
import java.util.function.Supplier
import org.jetbrains.exposed.sql.transactions.transaction as txn
val logger = KotlinLogging.logger { }
val lock = Object()
val threadPool: ExecutorService = Executors.newSingleThreadExecutor()
fun <T> asyncTransaction(db: Database? = null, statement: Transaction.() -> T): Future<T> {
return CompletableFuture.supplyAsync {
synchronized(lock) {
txn(db.transactionManager.defaultIsolationLevel, db.transactionManager.defaultRepetitionAttempts, db, statement)
}
}.exceptionally { t ->
logger.error(t) {
"Failed to run asyncTransaction"
}
null
fun <T> asyncTransaction(db: Database? = null, statement: Transaction.() -> T): Future<T?> {
val supplier = Supplier<T> {
txn(db.transactionManager.defaultIsolationLevel, db.transactionManager.defaultRepetitionAttempts, db, statement)
}
return CompletableFuture
.supplyAsync(supplier, threadPool)
.exceptionally { t ->
logger.error(t) { "Failed to run asyncTransaction" }
null
}
}
......@@ -19,7 +19,6 @@ import tech.gdragon.commands.misc.Stop
import tech.gdragon.commands.settings.*
import tech.gdragon.listener.EventListener
import tech.gdragon.listener.SystemEventListener
import java.util.*
import javax.security.auth.login.LoginException
class Bot : KoinComponent {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment