fix
All checks were successful
Deploy Dev / Build (pull_request) Successful in 7s
Deploy Dev / Push (pull_request) Successful in 8s
Deploy Dev / Deploy dev (pull_request) Successful in 11s

This commit is contained in:
emmatveev 2024-11-28 22:55:42 +03:00
parent e7ce0cb6af
commit 03fcd8894b

View File

@ -1,3 +1,4 @@
import multiprocessing
import telebot import telebot
import threading import threading
import time import time
@ -9,7 +10,7 @@ from utils import queues
class Daemon(base.Daemon): class Daemon(base.Daemon):
def __init__(self): def __init__(self):
self.telegram_bots: dict[str, dict[str, telebot.TeleBot|None]] = {} self.telegram_bots: dict[str, dict[str, multiprocessing.Process|None]] = {}
def execute(self): def execute(self):
while True: while True:
@ -22,26 +23,26 @@ class Daemon(base.Daemon):
self.telegram_bots[project_name][bot_name] = None self.telegram_bots[project_name][bot_name] = None
bot = self.telegram_bots[project_name][bot_name] bot = self.telegram_bots[project_name][bot_name]
if bot_info.get('poll_enabled'): if bot_info.get('poll_enabled'):
if bot is not None: if bot is not None and bot.is_alive:
print(f'process for {project_name} {bot_name} is alive') print(f'process for {project_name} {bot_name} is alive')
continue continue
print(f'starting process for {project_name} {bot_name}') print(f'starting process for {project_name} {bot_name}')
bot = telebot.TeleBot(bot_info['secrets']['telegram_token']) bot = telebot.TeleBot(bot_info['secrets']['telegram_token'])
self.start_polling(bot, bot_info['queue']) self.start_polling(bot, bot_info['queue'], project_name, bot_name)
self.telegram_bots[project_name][bot_name] = bot
print(f'started process for {project_name} {bot_name}') print(f'started process for {project_name} {bot_name}')
else: else:
if bot is None: if bot is None:
print(f'process for {project_name} {bot_name} is not alive') print(f'process for {project_name} {bot_name} is not alive')
continue continue
print(f'terminating process for {project_name} {bot_name}') print(f'terminating process for {project_name} {bot_name}')
bot.stop_bot() bot.terminate()
self.telegram_bots[project_name][bot_name] = None self.telegram_bots[project_name][bot_name] = None
print(f'terminated process for {project_name} {bot_name}') print(f'terminated process for {project_name} {bot_name}')
time.sleep(10) time.sleep(10)
def start_polling(self, bot: telebot.TeleBot, queue: str): def start_polling(self, bot: telebot.TeleBot, queue: str, project_name: str, bot_name: str):
@bot.message_handler() @bot.message_handler()
def do_action(message: telebot.types.Message): def do_action(message: telebot.types.Message):
queues.set_task(queue, message.json, 1) queues.set_task(queue, message.json, 1)
threading.Thread(target=bot.polling).start() process = multiprocessing.Process(target=bot.polling)
self.telegram_bots[project_name][bot_name] = process