fix
All checks were successful
Deploy Dev / Build (pull_request) Successful in 6s
Deploy Dev / Push (pull_request) Successful in 8s
Deploy Dev / Deploy dev (pull_request) Successful in 8s
Deploy Prod / Build (pull_request) Successful in 6s
Deploy Prod / Push (pull_request) Successful in 7s
Deploy Prod / Deploy prod (pull_request) Successful in 9s

This commit is contained in:
emmatveev 2024-11-28 23:07:50 +03:00
parent 03fcd8894b
commit aae0871671

View File

@ -1,4 +1,3 @@
import multiprocessing
import telebot import telebot
import threading import threading
import time import time
@ -10,7 +9,8 @@ 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, multiprocessing.Process|None]] = {} self.telegram_bots: dict[str, dict[str, telebot.TeleBot|None]] = {}
self.threads: dict[str, dict[str, threading.Thread|None]] = {}
def execute(self): def execute(self):
while True: while True:
@ -18,31 +18,36 @@ class Daemon(base.Daemon):
for project_name, project in bots.items(): for project_name, project in bots.items():
if project_name not in self.telegram_bots: if project_name not in self.telegram_bots:
self.telegram_bots[project_name] = {} self.telegram_bots[project_name] = {}
self.threads[project_name] = {}
for bot_name, bot_info in project.items(): for bot_name, bot_info in project.items():
if bot_name not in self.telegram_bots[project_name]: if bot_name not in self.telegram_bots[project_name]:
self.telegram_bots[project_name][bot_name] = None self.telegram_bots[project_name][bot_name] = None
self.threads[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 and bot.is_alive: if bot is not None and self.threads[project_name][bot_name].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'], project_name, bot_name) 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}') 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.terminate() bot.stop_bot()
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, project_name: str, bot_name: str): def start_polling(self, bot: telebot.TeleBot, queue: str) -> threading.Thread:
@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)
process = multiprocessing.Process(target=bot.polling) thread = threading.Thread(target=bot.polling)
self.telegram_bots[project_name][bot_name] = process thread.start()
return thread