Compare commits

..

No commits in common. "56eb2f60ee68c7905928e97c5a61b90383227565" and "cf1f92dcc9ba59ebeb6b4c0281462aa11d64f84d" have entirely different histories.

View File

@ -1,5 +1,5 @@
import telebot import telebot
import multiprocessing import threading
import time import time
from daemons import base from daemons import base
@ -10,37 +10,45 @@ from utils import queues
class Daemon(base.Daemon): class Daemon(base.Daemon):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.processes: 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:
bots = platform.platform_client.get_config('bots') bots = platform.platform_client.get_config('bots')
for project_name, project in bots.items(): 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(): for bot_name, bot_info in project.items():
key = f'{project_name}_{bot_name}' if bot_name not in self.telegram_bots[project_name]:
proc = self.processes.get(key) 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_info.get('poll_enabled'):
if proc and proc.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}')
process = multiprocessing.Process(target=self.start_polling, args=(bot_info['secrets']['telegram_token'], bot_info['queue'])) bot = telebot.TeleBot(bot_info['secrets']['telegram_token'])
process.start() thread = self.start_polling(bot, bot_info['queue'])
self.processes[key] = process 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 proc 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}')
proc.terminate() bot.stop_bot()
self.processes[key] = 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, token: str, queue: str): def start_polling(self, bot: telebot.TeleBot, queue: str) -> threading.Thread:
bot = telebot.TeleBot(token)
@bot.message_handler(content_types=['audio', 'photo', 'voice', 'video', 'document', 'animation', 'text', 'location', 'contact', 'sticker', 'video_note']) @bot.message_handler(content_types=['audio', 'photo', 'voice', 'video', 'document', 'animation', 'text', 'location', 'contact', 'sticker', 'video_note'])
def do_action(message: telebot.types.Message): def do_action(message: telebot.types.Message):
queues.set_task(self.stub, queue, message.json, 1) queues.set_task(self.stub, queue, message.json, 1)
bot.polling() thread = threading.Thread(target=bot.polling)
thread.start()
return thread