[hotfix] coroutine related fixed. (2x)

This commit is contained in:
dalbodeule 2025-06-08 15:24:33 +09:00
parent a88f994ccd
commit 722b5972d9
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65

View File

@ -1,12 +1,7 @@
package space.mori.chzzk_bot.chatbot.chzzk package space.mori.chzzk_bot.chatbot.chzzk
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.*
import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.future.await
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.koin.java.KoinJavaComponent.inject import org.koin.java.KoinJavaComponent.inject
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -18,7 +13,8 @@ import space.mori.chzzk_bot.common.models.User
import space.mori.chzzk_bot.common.services.LiveStatusService import space.mori.chzzk_bot.common.services.LiveStatusService
import space.mori.chzzk_bot.common.services.TimerConfigService import space.mori.chzzk_bot.common.services.TimerConfigService
import space.mori.chzzk_bot.common.services.UserService import space.mori.chzzk_bot.common.services.UserService
import space.mori.chzzk_bot.common.utils.* import space.mori.chzzk_bot.common.utils.getChzzkChannelId
import space.mori.chzzk_bot.common.utils.getUptime
import xyz.r2turntrue.chzzk4j.ChzzkClient import xyz.r2turntrue.chzzk4j.ChzzkClient
import xyz.r2turntrue.chzzk4j.session.ChzzkSessionBuilder import xyz.r2turntrue.chzzk4j.session.ChzzkSessionBuilder
import xyz.r2turntrue.chzzk4j.session.ChzzkSessionSubscriptionType import xyz.r2turntrue.chzzk4j.session.ChzzkSessionSubscriptionType
@ -26,10 +22,11 @@ import xyz.r2turntrue.chzzk4j.session.ChzzkUserSession
import xyz.r2turntrue.chzzk4j.session.event.SessionChatMessageEvent import xyz.r2turntrue.chzzk4j.session.event.SessionChatMessageEvent
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveDetail import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveDetail
import java.lang.Exception import java.lang.Runnable
import java.lang.Thread
import java.net.SocketTimeoutException import java.net.SocketTimeoutException
import java.time.LocalDateTime
import java.nio.charset.Charset import java.nio.charset.Charset
import java.time.LocalDateTime
object ChzzkHandler { object ChzzkHandler {
private val handlers = mutableListOf<UserHandler>() private val handlers = mutableListOf<UserHandler>()
@ -119,7 +116,7 @@ object ChzzkHandler {
try { try {
it.isActive(true, streamInfo) it.isActive(true, streamInfo)
} catch(e: Exception) { } catch(e: Exception) {
logger.info("Exception: ${e.stackTraceToString()}") logger.info("Thread 1 Exception: ${e.stackTraceToString()}")
} }
} }
if (streamInfo?.isOnline == false && it.isActive) it.isActive(false, streamInfo) if (streamInfo?.isOnline == false && it.isActive) it.isActive(false, streamInfo)
@ -147,14 +144,14 @@ object ChzzkHandler {
try { try {
it.isActive(true, streamInfo) it.isActive(true, streamInfo)
} catch(e: Exception) { } catch(e: Exception) {
logger.info("Exception: ${e.stackTraceToString()}") logger.info("Thread 2 Exception: ${e.stackTraceToString()}")
} }
} }
if (streamInfo?.isOnline == false && it.isActive) it.isActive(false, streamInfo) if (streamInfo?.isOnline == false && it.isActive) it.isActive(false, streamInfo)
} catch (e: SocketTimeoutException) { } catch (e: SocketTimeoutException) {
logger.info("Thread 1 Timeout: ${it.channel.channelName} / ${e.stackTraceToString()}") logger.info("Thread 2 Timeout: ${it.channel.channelName} / ${e.stackTraceToString()}")
} catch (e: Exception) { } catch (e: Exception) {
logger.info("Thread 1 Exception: ${it.channel.channelName} / ${e.stackTraceToString()}") logger.info("Thread 2 Exception: ${it.channel.channelName} / ${e.stackTraceToString()}")
} finally { } finally {
Thread.sleep(5000) Thread.sleep(5000)
} }
@ -254,7 +251,7 @@ class UserHandler(
} }
} }
private suspend fun connect() = runBlocking { private suspend fun connect() {
val tokens = user.refreshToken?.let { token -> Connector.client.refreshAccessToken(token)} val tokens = user.refreshToken?.let { token -> Connector.client.refreshAccessToken(token)}
if(tokens == null) { if(tokens == null) {
throw RuntimeException("AccessToken is not valid.") throw RuntimeException("AccessToken is not valid.")
@ -263,9 +260,9 @@ class UserHandler(
UserService.setRefreshToken(user, tokens.first, tokens.second) UserService.setRefreshToken(user, tokens.first, tokens.second)
chatChannelId = getChzzkChannelId(channel.channelId) ?: throw RuntimeException("Chat Channel ID is not found.") chatChannelId = getChzzkChannelId(channel.channelId) ?: throw RuntimeException("Chat Channel ID is not found.")
client.loginAsync().join() client.loginAsync().await()
listener = ChzzkSessionBuilder(client).buildUserSession() listener = ChzzkSessionBuilder(client).buildUserSession()
listener.createAndConnectAsync().join() listener.createAndConnectAsync().await()
messageHandler = MessageHandler(this@UserHandler) messageHandler = MessageHandler(this@UserHandler)
} }