Now using coroutines in ThreadedModule.

This commit is contained in:
Erik C. Thauvin 2021-09-15 13:03:01 -07:00
parent f4c6bf48fa
commit 25a0850b71
5 changed files with 20 additions and 10 deletions

2
.idea/misc.xml generated
View file

@ -4,5 +4,5 @@
<pattern value="net.thauvin.erik.mobibot.modules.War" method="War" />
</component>
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="16" project-jdk-type="JavaSDK" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="15" project-jdk-type="JavaSDK" />
</project>

View file

@ -2,7 +2,7 @@ plugins {
id 'application'
id 'com.github.ben-manes.versions' version '0.39.0'
id 'idea'
id 'io.gitlab.arturbosch.detekt' version '1.18.0'
id 'io.gitlab.arturbosch.detekt' version '1.18.1'
id 'jacoco'
id 'java'
id 'net.thauvin.erik.gradle.semver' version '1.0.4'
@ -39,7 +39,7 @@ dependencies {
compileOnly 'pircbot:pircbot:1.5.0:sources'
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
implementation "org.apache.logging.log4j:log4j-api:$versions.log4j"
implementation "org.apache.logging.log4j:log4j-core:$versions.log4j"
@ -97,6 +97,7 @@ kotlin {
}
kapt {
includeCompileClasspath = false
arguments {
arg('semver.project.dir', projectDir)
}

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.mobibot
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
@ -47,12 +48,14 @@ import java.util.Date
* Handles posts to pinboard.in.
*/
object PinboardUtils {
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
/**
* Adds a pin.
*/
@JvmStatic
fun addPin(poster: PinboardPoster, ircServer: String, entry: EntryLink) = runBlocking {
withContext(Dispatchers.Default) {
withContext(dispatcher) {
poster.addPin(
entry.link,
entry.title,
@ -68,7 +71,7 @@ object PinboardUtils {
*/
@JvmStatic
fun deletePin(poster: PinboardPoster, entry: EntryLink) = runBlocking {
withContext(Dispatchers.Default) {
withContext(dispatcher) {
poster.deletePin(entry.link)
}
}
@ -78,7 +81,7 @@ object PinboardUtils {
*/
@JvmStatic
fun updatePin(poster: PinboardPoster, ircServer: String, oldUrl: String, entry: EntryLink) = runBlocking {
withContext(Dispatchers.Default) {
withContext(dispatcher) {
with(entry) {
if (oldUrl != link) {
poster.deletePin(oldUrl)

View file

@ -31,6 +31,8 @@
*/
package net.thauvin.erik.mobibot.modules
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.thauvin.erik.mobibot.Mobibot
/**
@ -44,7 +46,11 @@ abstract class ThreadedModule(bot: Mobibot) : AbstractModule(bot) {
isPrivate: Boolean
) {
if (isEnabled && args.isNotEmpty()) {
Thread { run(sender, cmd, args, isPrivate) }.start()
runBlocking {
launch {
run(sender, cmd, args, isPrivate)
}
}
} else {
helpResponse(sender, isPrivate)
}

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle
#Tue Aug 24 11:34:08 PDT 2021
version.buildmeta=1366
#Wed Sep 15 13:00:35 PDT 2021
version.buildmeta=1390
version.major=0
version.minor=8
version.patch=0
version.prerelease=beta
version.project=mobibot
version.semver=0.8.0-beta+1366
version.semver=0.8.0-beta+1390