Compare commits

..

No commits in common. "05d9cdc7b1b34e03deedf825f81b26a85ce5c138" and "a2092465138aa1778a822130865b545892f03eb2" have entirely different histories.

View File

@ -1,4 +1,3 @@
import multiprocessing
import telebot import telebot
import threading import threading
import time import time
@ -10,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, multiprocessing.Process|None]] = {} self.telegram_bots: dict[str, dict[str, telebot.TeleBot|None]] = {}
def execute(self): def execute(self):
while True: while True:
@ -23,26 +22,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 and bot.is_alive: if bot is not None:
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) 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 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):
@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) threading.Thread(target=bot.polling).start()
self.telegram_bots[project_name][bot_name] = process