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

This commit is contained in:
emmatveev 2024-11-27 14:45:46 +03:00
parent 1b788ff2b2
commit 4042ce13d0

View File

@ -9,7 +9,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, tuple[telebot.TeleBot, threading.Thread]|None]] = {} self.telegram_bots: dict[str, dict[str, telebot.TeleBot|None]] = {}
def execute(self): def execute(self):
while True: while True:
@ -20,31 +20,28 @@ class Daemon(base.Daemon):
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
internal_bot_info = 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 internal_bot_info is not None: if bot is not None:
bot, thread = internal_bot_info print(f'process for {project_name} {bot_name} is alive')
if thread.is_alive: continue
print(f'process for {project_name} {bot_name} is alive')
continue
bot = telebot.TeleBot(bot_info['secrets']['telegram_token'])
thread = threading.Thread(target=self.start_polling, args=[bot, bot_info['queue']])
print(f'starting process for {project_name} {bot_name}') print(f'starting process for {project_name} {bot_name}')
thread.start() bot = telebot.TeleBot(bot_info['secrets']['telegram_token'])
self.telegram_bots[project_name][bot_name] = (bot, thread) self.start_polling(bot, bot_info['queue'])
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 internal_bot_info is None or not internal_bot_info[1].is_alive: 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}')
internal_bot_info[0].stop_bot() 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, queue): def start_polling(self, bot: telebot.TeleBot, queue: str):
@bot.message_handler() @bot.message_handler()
def do_action(message): def do_action(message: telebot.types.Message):
queues.set_task(queue, message.json, 1) queues.set_task(queue, message.json, 1)
bot.polling() threading.Thread(target=bot.polling).start()