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
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import net.thauvin.erik.mobibot.entries.EntryLink
import net.thauvin.erik.pinboard.PinboardPoster
import java.time.ZoneId
@ -48,21 +46,21 @@ 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(dispatcher) {
poster.addPin(
entry.link,
entry.title,
entry.postedBy(ircServer),
entry.pinboardTags,
entry.date.toTimestamp()
)
fun addPin(poster: PinboardPoster, ircServer: String, entry: EntryLink) {
runBlocking {
launch {
poster.addPin(
entry.link,
entry.title,
entry.postedBy(ircServer),
entry.pinboardTags,
entry.date.toTimestamp()
)
}
}
}
@ -70,9 +68,11 @@ object PinboardUtils {
* Deletes a pin.
*/
@JvmStatic
fun deletePin(poster: PinboardPoster, entry: EntryLink) = runBlocking {
withContext(dispatcher) {
poster.deletePin(entry.link)
fun deletePin(poster: PinboardPoster, entry: EntryLink) {
runBlocking {
launch {
poster.deletePin(entry.link)
}
}
}
@ -80,28 +80,30 @@ object PinboardUtils {
* Updates a pin.
*/
@JvmStatic
fun updatePin(poster: PinboardPoster, ircServer: String, oldUrl: String, entry: EntryLink) = runBlocking {
withContext(dispatcher) {
with(entry) {
if (oldUrl != link) {
poster.deletePin(oldUrl)
poster.addPin(
link,
title,
entry.postedBy(ircServer),
pinboardTags,
date.toTimestamp()
)
} else {
poster.addPin(
link,
title,
entry.postedBy(ircServer),
pinboardTags,
date.toTimestamp(),
replace = true,
shared = true
)
fun updatePin(poster: PinboardPoster, ircServer: String, oldUrl: String, entry: EntryLink) {
runBlocking {
launch {
with(entry) {
if (oldUrl != link) {
poster.deletePin(oldUrl)
poster.addPin(
link,
title,
entry.postedBy(ircServer),
pinboardTags,
date.toTimestamp()
)
} else {
poster.addPin(
link,
title,
entry.postedBy(ircServer),
pinboardTags,
date.toTimestamp(),
replace = true,
shared = true
)
}
}
}
}

View file

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

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
import net.thauvin.erik.mobibot.Utils.cyan
import net.thauvin.erik.mobibot.Utils.helpFormat
@ -52,7 +54,9 @@ class Joke(bot: Mobibot) : ThreadedModule(bot) {
args: String,
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
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.thauvin.erik.mobibot.Constants
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.TwitterTimer
@ -87,14 +89,16 @@ class Twitter(bot: Mobibot) : ThreadedModule(bot) {
fun notification(msg: String) {
with(bot) {
if (isEnabled && !handle.isNullOrBlank()) {
Thread {
try {
post(message = msg, isDm = true)
if (logger.isDebugEnabled) logger.debug("Notified @$handle: $msg")
} catch (e: ModuleException) {
if (logger.isWarnEnabled) logger.warn("Failed to notify @$handle: $msg", e)
runBlocking {
launch {
try {
post(message = msg, isDm = true)
if (logger.isDebugEnabled) logger.debug("Notified @$handle: $msg")
} 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) {
val entry = LinksMgr.entries[index]
val msg = "${entry.title} ${entry.link} via ${entry.nick} on $channel"
Thread {
try {
if (logger.isDebugEnabled) {
logger.debug("Posting {} to Twitter.", EntriesUtils.buildLinkCmd(index))
runBlocking {
launch {
try {
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)
}
}

View file

@ -50,18 +50,18 @@ class PinboardUtilsTest : LocalProperties() {
val ircServer = "irc.test.com"
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")
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")
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(PinboardUtils.deletePin(pinboard, entry), "daletePin")
PinboardUtils.deletePin(pinboard, entry)
assertFalse(validatePin(apiToken, url = entry.link), "validate delete")
}

View file

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