Renaming some files. Re-create a new thread and fix: raise RuntimeError("threads can only be started once")

This commit is contained in:
Alessandro Maggio
2021-03-10 22:38:02 +01:00
parent 46918fda9b
commit 7013df0148
5 changed files with 86 additions and 75 deletions

View File

@@ -11,6 +11,7 @@ import uuid
from collections import OrderedDict
from datetime import datetime
from TwitchChannelPointsMiner.classes.Chat import ThreadChat
from TwitchChannelPointsMiner.classes.entities.PubsubTopic import PubsubTopic
from TwitchChannelPointsMiner.classes.entities.Streamer import (
Streamer,
@@ -19,7 +20,6 @@ from TwitchChannelPointsMiner.classes.entities.Streamer import (
from TwitchChannelPointsMiner.classes.Exceptions import StreamerDoesNotExistException
from TwitchChannelPointsMiner.classes.Settings import Priority, Settings
from TwitchChannelPointsMiner.classes.Twitch import Twitch
from TwitchChannelPointsMiner.classes.TwitchChat import TwitchChat
from TwitchChannelPointsMiner.classes.WebSocketsPool import WebSocketsPool
from TwitchChannelPointsMiner.logger import LoggerSettings, configure_loggers
from TwitchChannelPointsMiner.utils import (
@@ -168,7 +168,7 @@ class TwitchChannelPointsMiner:
streamer.settings.bet, Settings.streamer_settings.bet
)
if streamer.settings.join_chat is True:
streamer.irc_chat = TwitchChat(
streamer.irc_chat = ThreadChat(
self.username,
self.twitch.twitch_login.get_auth_token(),
streamer.username,

View File

@@ -0,0 +1,75 @@
import logging
import time
from threading import Thread
from irc.bot import SingleServerIRCBot
from TwitchChannelPointsMiner.constants import IRC, IRC_PORT
logger = logging.getLogger(__name__)
class ClientIRC(SingleServerIRCBot):
def __init__(self, username, token, channel):
self.token = token
self.channel = "#" + channel
self.__active = False
super(ClientIRC, self).__init__(
[(IRC, IRC_PORT, f"oauth:{token}")], username, username
)
def on_welcome(self, client, event):
client.join(self.channel)
def start(self):
self.__active = True
self._connect()
while self.__active:
try:
self.reactor.process_once(timeout=0.2)
time.sleep(0.01)
except Exception as e:
logger.error(
f"Exception raised: {e}. Thread is active: {self.__active}"
)
def die(self, msg="Bye, cruel world!"):
self.connection.disconnect(msg)
self.__active = False
"""
def on_join(self, connection, event):
logger.info(f"Event: {event}", extra={"emoji": ":speech_balloon:"})
def on_pubmsg(self, client, message):
logger.info(f"Message: {message}", extra={"emoji": ":speech_balloon:"})
"""
class ThreadChat(Thread):
def __deepcopy__(self, memo):
return None
def __init__(self, username, token, channel):
super(ThreadChat, self).__init__()
self.username = username
self.token = token
self.channel = channel
self.chat_irc = None
def run(self):
self.chat_irc = ClientIRC(self.username, self.token, self.channel)
logger.info(
f"Join IRC Chat: {self.channel}", extra={"emoji": ":speech_balloon:"}
)
self.chat_irc.start()
def stop(self):
if self.chat_irc is not None:
logger.info(
f"Leave IRC Chat: {self.channel}", extra={"emoji": ":speech_balloon:"}
)
self.chat_irc.die()

View File

@@ -1,34 +0,0 @@
import logging
from threading import Thread
from TwitchChannelPointsMiner.classes.entities.Chat import Chat
logger = logging.getLogger(__name__)
class TwitchChat(Thread):
def __deepcopy__(self, memo):
return None
def __init__(self, username, token, channel):
super(TwitchChat, self).__init__()
self.token = token
self.username = username
self.channel = channel
self.chat_irc = None
def run(self):
self.chat_irc = Chat(self.username, self.token, self.channel)
logger.info(
f"Join IRC Chat: {self.channel}", extra={"emoji": ":speech_balloon:"}
)
self.chat_irc.start()
def stop(self):
if self.chat_irc is not None:
logger.info(
f"Leave IRC Chat: {self.channel}", extra={"emoji": ":speech_balloon:"}
)
self.chat_irc.die()

View File

@@ -1,39 +0,0 @@
import logging
from irc.bot import SingleServerIRCBot
from TwitchChannelPointsMiner.constants import IRC, IRC_PORT
logger = logging.getLogger(__name__)
class Chat(SingleServerIRCBot):
def __init__(self, username, token, channel):
self.token = token
self.channel = "#" + channel
self.__active = False
super(Chat, self).__init__(
[(IRC, IRC_PORT, f"oauth:{token}")], username, username
)
def on_welcome(self, client, event):
client.join(self.channel)
def start(self):
self.__active = True
self._connect()
while self.__active:
self.reactor.process_once(timeout=0.2)
def die(self, msg="Bye, cruel world!"):
self.connection.disconnect(msg)
self.__active = False
"""
def on_join(self, connection, event):
logger.info(f"Event: {event}", extra={"emoji": ":speech_balloon:"})
def on_pubmsg(self, client, message):
logger.info(f"Message: {message}", extra={"emoji": ":speech_balloon:"})
"""

View File

@@ -1,6 +1,7 @@
import logging
import time
from TwitchChannelPointsMiner.classes.Chat import ThreadChat
from TwitchChannelPointsMiner.classes.entities.Bet import BetSettings
from TwitchChannelPointsMiner.classes.entities.Stream import Stream
from TwitchChannelPointsMiner.classes.Settings import Settings
@@ -164,6 +165,14 @@ class Streamer(object):
if self.irc_chat is not None:
self.irc_chat.stop()
# Recreate a new thread to start again
# raise RuntimeError("threads can only be started once")
self.irc_chat = ThreadChat(
self.irc_chat.username,
self.irc_chat.token,
self.username,
)
def join_chat(self):
if self.irc_chat is not None:
self.irc_chat.start()