Converted all Thread() to coroutines.

This commit is contained in:
Erik C. Thauvin 2021-09-15 16:38:36 -07:00
parent 25a0850b71
commit 48b5c8a345
6 changed files with 81 additions and 63 deletions

View file

@ -32,10 +32,8 @@
package net.thauvin.erik.mobibot package net.thauvin.erik.mobibot
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.launch
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import net.thauvin.erik.mobibot.entries.EntryLink import net.thauvin.erik.mobibot.entries.EntryLink
import net.thauvin.erik.pinboard.PinboardPoster import net.thauvin.erik.pinboard.PinboardPoster
import java.time.ZoneId import java.time.ZoneId
@ -48,21 +46,21 @@ import java.util.Date
* Handles posts to pinboard.in. * Handles posts to pinboard.in.
*/ */
object PinboardUtils { object PinboardUtils {
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
/** /**
* Adds a pin. * Adds a pin.
*/ */
@JvmStatic @JvmStatic
fun addPin(poster: PinboardPoster, ircServer: String, entry: EntryLink) = runBlocking { fun addPin(poster: PinboardPoster, ircServer: String, entry: EntryLink) {
withContext(dispatcher) { runBlocking {
poster.addPin( launch {
entry.link, poster.addPin(
entry.title, entry.link,
entry.postedBy(ircServer), entry.title,
entry.pinboardTags, entry.postedBy(ircServer),
entry.date.toTimestamp() entry.pinboardTags,
) entry.date.toTimestamp()
)
}
} }
} }
@ -70,9 +68,11 @@ object PinboardUtils {
* Deletes a pin. * Deletes a pin.
*/ */
@JvmStatic @JvmStatic
fun deletePin(poster: PinboardPoster, entry: EntryLink) = runBlocking { fun deletePin(poster: PinboardPoster, entry: EntryLink) {
withContext(dispatcher) { runBlocking {
poster.deletePin(entry.link) launch {
poster.deletePin(entry.link)
}
} }
} }
@ -80,28 +80,30 @@ object PinboardUtils {
* Updates a pin. * Updates a pin.
*/ */
@JvmStatic @JvmStatic
fun updatePin(poster: PinboardPoster, ircServer: String, oldUrl: String, entry: EntryLink) = runBlocking { fun updatePin(poster: PinboardPoster, ircServer: String, oldUrl: String, entry: EntryLink) {
withContext(dispatcher) { runBlocking {
with(entry) { launch {
if (oldUrl != link) { with(entry) {
poster.deletePin(oldUrl) if (oldUrl != link) {
poster.addPin( poster.deletePin(oldUrl)
link, poster.addPin(
title, link,
entry.postedBy(ircServer), title,
pinboardTags, entry.postedBy(ircServer),
date.toTimestamp() pinboardTags,
) date.toTimestamp()
} else { )
poster.addPin( } else {
link, poster.addPin(
title, link,
entry.postedBy(ircServer), title,
pinboardTags, entry.postedBy(ircServer),
date.toTimestamp(), pinboardTags,
replace = true, date.toTimestamp(),
shared = true replace = true,
) shared = true
)
}
} }
} }
} }

View file

@ -32,6 +32,8 @@
package net.thauvin.erik.mobibot.commands package net.thauvin.erik.mobibot.commands
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.thauvin.erik.mobibot.FeedReader import net.thauvin.erik.mobibot.FeedReader
import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.helpFormat
@ -60,7 +62,11 @@ class ChannelFeed(bot: Mobibot, channel: String) : AbstractCommand(bot) {
) { ) {
with(properties[FEED_PROP]) { with(properties[FEED_PROP]) {
if (!isNullOrBlank()) { if (!isNullOrBlank()) {
Thread(FeedReader(bot, sender, this)).start() runBlocking {
launch {
FeedReader(bot, sender, this@with).run()
}
}
} else { } else {
bot.send(sender, "There is no feed setup for this channel.", false) bot.send(sender, "There is no feed setup for this channel.", false)
} }

View file

@ -31,6 +31,8 @@
*/ */
package net.thauvin.erik.mobibot.modules package net.thauvin.erik.mobibot.modules
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils.cyan import net.thauvin.erik.mobibot.Utils.cyan
import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.Utils.helpFormat
@ -52,7 +54,9 @@ class Joke(bot: Mobibot) : ThreadedModule(bot) {
args: String, args: String,
isPrivate: Boolean isPrivate: Boolean
) { ) {
Thread { run(sender, cmd, args, isPrivate) }.start() runBlocking {
launch { run(sender, cmd, args, isPrivate) }
}
} }
/** /**

View file

@ -31,6 +31,8 @@
*/ */
package net.thauvin.erik.mobibot.modules package net.thauvin.erik.mobibot.modules
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.thauvin.erik.mobibot.Constants import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.TwitterTimer import net.thauvin.erik.mobibot.TwitterTimer
@ -87,14 +89,16 @@ class Twitter(bot: Mobibot) : ThreadedModule(bot) {
fun notification(msg: String) { fun notification(msg: String) {
with(bot) { with(bot) {
if (isEnabled && !handle.isNullOrBlank()) { if (isEnabled && !handle.isNullOrBlank()) {
Thread { runBlocking {
try { launch {
post(message = msg, isDm = true) try {
if (logger.isDebugEnabled) logger.debug("Notified @$handle: $msg") post(message = msg, isDm = true)
} catch (e: ModuleException) { if (logger.isDebugEnabled) logger.debug("Notified @$handle: $msg")
if (logger.isWarnEnabled) logger.warn("Failed to notify @$handle: $msg", e) } catch (e: ModuleException) {
if (logger.isWarnEnabled) logger.warn("Failed to notify @$handle: $msg", e)
}
} }
}.start() }
} }
} }
} }
@ -123,16 +127,18 @@ class Twitter(bot: Mobibot) : ThreadedModule(bot) {
if (isAutoPost && hasEntry(index) && LinksMgr.entries.size >= index) { if (isAutoPost && hasEntry(index) && LinksMgr.entries.size >= index) {
val entry = LinksMgr.entries[index] val entry = LinksMgr.entries[index]
val msg = "${entry.title} ${entry.link} via ${entry.nick} on $channel" val msg = "${entry.title} ${entry.link} via ${entry.nick} on $channel"
Thread { runBlocking {
try { launch {
if (logger.isDebugEnabled) { try {
logger.debug("Posting {} to Twitter.", EntriesUtils.buildLinkCmd(index)) if (logger.isDebugEnabled) {
logger.debug("Posting {} to Twitter.", EntriesUtils.buildLinkCmd(index))
}
post(message = msg, isDm = false)
} catch (e: ModuleException) {
if (logger.isWarnEnabled) logger.warn("Failed to post entry on Twitter.", e)
} }
post(message = msg, isDm = false)
} catch (e: ModuleException) {
if (logger.isWarnEnabled) logger.warn("Failed to post entry on Twitter.", e)
} }
}.start() }
removeEntry(index) removeEntry(index)
} }
} }

View file

@ -50,18 +50,18 @@ class PinboardUtilsTest : LocalProperties() {
val ircServer = "irc.test.com" val ircServer = "irc.test.com"
val entry = EntryLink(url, "Test Example", "ErikT", "", "#mobitopia", listOf("test")) val entry = EntryLink(url, "Test Example", "ErikT", "", "#mobitopia", listOf("test"))
assertTrue(PinboardUtils.addPin(pinboard, ircServer, entry), "addPin") PinboardUtils.addPin(pinboard, ircServer, entry)
assertTrue(validatePin(apiToken, url = entry.link, entry.title, entry.nick, entry.channel), "validate add") assertTrue(validatePin(apiToken, url = entry.link, entry.title, entry.nick, entry.channel), "validate add")
entry.link = "https://www.foo.com/" entry.link = "https://www.foo.com/"
assertTrue(PinboardUtils.updatePin(pinboard, ircServer, url, entry), "updatePin") PinboardUtils.updatePin(pinboard, ircServer, url, entry)
assertTrue(validatePin(apiToken, url = entry.link, ircServer), "validate update") assertTrue(validatePin(apiToken, url = entry.link, ircServer), "validate update")
entry.title = "Foo Title" entry.title = "Foo Title"
assertTrue(PinboardUtils.updatePin(pinboard, ircServer, entry.link, entry), "update title") PinboardUtils.updatePin(pinboard, ircServer, entry.link, entry)
assertTrue(validatePin(apiToken, url = entry.link, entry.title), "validate title") assertTrue(validatePin(apiToken, url = entry.link, entry.title), "validate title")
assertTrue(PinboardUtils.deletePin(pinboard, entry), "daletePin") PinboardUtils.deletePin(pinboard, entry)
assertFalse(validatePin(apiToken, url = entry.link), "validate delete") assertFalse(validatePin(apiToken, url = entry.link), "validate delete")
} }

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle #Generated by the Semver Plugin for Gradle
#Wed Sep 15 13:00:35 PDT 2021 #Wed Sep 15 17:27:25 PDT 2021
version.buildmeta=1390 version.buildmeta=1400
version.major=0 version.major=0
version.minor=8 version.minor=8
version.patch=0 version.patch=0
version.prerelease=beta version.prerelease=beta
version.project=mobibot version.project=mobibot
version.semver=0.8.0-beta+1390 version.semver=0.8.0-beta+1400