diff --git a/daemons/poll.py b/daemons/poll.py index 412c4bd..978bae4 100644 --- a/daemons/poll.py +++ b/daemons/poll.py @@ -10,6 +10,7 @@ from utils import queues class Daemon(base.Daemon): def __init__(self): self.telegram_bots: dict[str, dict[str, telebot.TeleBot|None]] = {} + self.threads: dict[str, dict[str, threading.Thread|None]] = {} def execute(self): while True: @@ -17,18 +18,21 @@ class Daemon(base.Daemon): for project_name, project in bots.items(): if project_name not in self.telegram_bots: self.telegram_bots[project_name] = {} + self.threads[project_name] = {} for bot_name, bot_info in project.items(): if bot_name not in self.telegram_bots[project_name]: self.telegram_bots[project_name][bot_name] = None + self.threads[project_name][bot_name] = None bot = self.telegram_bots[project_name][bot_name] if bot_info.get('poll_enabled'): - if bot is not None: + if bot is not None and self.threads[project_name][bot_name].is_alive(): print(f'process for {project_name} {bot_name} is alive') continue print(f'starting process for {project_name} {bot_name}') bot = telebot.TeleBot(bot_info['secrets']['telegram_token']) - self.start_polling(bot, bot_info['queue']) + thread = self.start_polling(bot, bot_info['queue']) self.telegram_bots[project_name][bot_name] = bot + self.threads[project_name][bot_name] = thread print(f'started process for {project_name} {bot_name}') else: if bot is None: @@ -40,8 +44,10 @@ class Daemon(base.Daemon): print(f'terminated process for {project_name} {bot_name}') time.sleep(10) - def start_polling(self, bot: telebot.TeleBot, queue: str): + def start_polling(self, bot: telebot.TeleBot, queue: str) -> threading.Thread: @bot.message_handler() def do_action(message: telebot.types.Message): queues.set_task(queue, message.json, 1) - threading.Thread(target=bot.polling).start() + thread = threading.Thread(target=bot.polling) + thread.start() + return thread