-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ThreadPoolManager for command and message processing
- Loading branch information
1 parent
1285bee
commit 15c9815
Showing
4 changed files
with
68 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
foxy/src/main/kotlin/net/cakeyfox/foxy/utils/threads/ThreadPoolManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package net.cakeyfox.foxy.utils.threads | ||
|
||
import kotlinx.coroutines.* | ||
import mu.KotlinLogging | ||
import net.dv8tion.jda.api.events.Event | ||
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent | ||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent | ||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent | ||
import java.util.concurrent.ExecutorService | ||
|
||
class ThreadPoolManager { | ||
private val coroutineMessageExecutor: ExecutorService = ThreadUtils.createThreadPool("MessageExecutor [%d]") | ||
private val coroutineMessageDispatcher = coroutineMessageExecutor.asCoroutineDispatcher() | ||
private val activeJobs = ThreadUtils.activeJobs | ||
|
||
@OptIn(DelicateCoroutinesApi::class) | ||
fun launchMessageJob(event: Event, block: suspend CoroutineScope.() -> Unit) { | ||
val coroutineName = when (event) { | ||
is MessageReceivedEvent -> "Message ${event.message} by user ${event.author}" | ||
is SlashCommandInteractionEvent -> "Slash Command ${event.fullCommandName} by user ${event.user}" | ||
is MessageContextInteractionEvent -> "User Command ${event.fullCommandName} by user ${event.user}" | ||
else -> throw IllegalArgumentException("Event $event is not supported") | ||
} | ||
|
||
val start = System.currentTimeMillis() | ||
val job = GlobalScope.launch( | ||
coroutineMessageDispatcher + CoroutineName(coroutineName), | ||
block = block | ||
) | ||
|
||
activeJobs.add(job) | ||
job.invokeOnCompletion { | ||
activeJobs.remove(job) | ||
val end = System.currentTimeMillis() | ||
val time = end - start | ||
if (time > 10_000) { | ||
KotlinLogging.logger("MessageExecutor").warn { "Job $job took ${time}ms to complete" } | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
foxy/src/main/kotlin/net/cakeyfox/foxy/utils/threads/ThreadUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package net.cakeyfox.foxy.utils.threads | ||
|
||
import com.google.common.util.concurrent.ThreadFactoryBuilder | ||
import kotlinx.coroutines.Job | ||
import java.util.concurrent.ConcurrentLinkedQueue | ||
import java.util.concurrent.ExecutorService | ||
import java.util.concurrent.Executors | ||
|
||
object ThreadUtils { | ||
fun createThreadPool(name: String): ExecutorService = | ||
Executors.newCachedThreadPool(ThreadFactoryBuilder().setNameFormat(name).build()) | ||
|
||
val activeJobs = ConcurrentLinkedQueue<Job>() | ||
} |