Converted all Thread() to coroutines.
This commit is contained in:
parent
25a0850b71
commit
48b5c8a345
6 changed files with 81 additions and 63 deletions
|
@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue