fix
This commit is contained in:
parent
8a01f9ce5d
commit
e0db9bb605
2
.gitignore
vendored
2
.gitignore
vendored
@ -117,3 +117,5 @@ GitHub.sublime-settings
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
.history
|
||||
|
||||
local_platform.json
|
||||
|
@ -1,5 +1,5 @@
|
||||
import telebot
|
||||
import multiprocessing
|
||||
import threading
|
||||
import time
|
||||
|
||||
from daemons import base
|
||||
@ -9,7 +9,7 @@ from utils import queues
|
||||
|
||||
class Daemon(base.Daemon):
|
||||
def __init__(self):
|
||||
self.telegram_pollers: dict[str, dict[str, multiprocessing.Process|None]] = {}
|
||||
self.telegram_bots: dict[str, dict[str, tuple[telebot.TeleBot, threading.Thread]|None]] = {}
|
||||
|
||||
def execute(self):
|
||||
while True:
|
||||
@ -20,28 +20,30 @@ class Daemon(base.Daemon):
|
||||
for bot_name, bot_info in project.items():
|
||||
if bot_name not in self.telegram_pollers[project_name]:
|
||||
self.telegram_pollers[project_name][bot_name] = None
|
||||
process = self.telegram_pollers[project_name][bot_name]
|
||||
internal_bot_info = self.telegram_pollers[project_name][bot_name]
|
||||
if bot_info.get('poll_enabled'):
|
||||
if process is not None and process.is_alive:
|
||||
print(f'process for {project_name} {bot_name} is alive')
|
||||
continue
|
||||
new_process = multiprocessing.Process(target=self.start_polling, args=[bot_info['secrets']['telegram_token'], bot_info['queue']])
|
||||
if internal_bot_info is not None:
|
||||
bot, thread = internal_bot_info
|
||||
if thread.is_alive:
|
||||
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}')
|
||||
new_process.start()
|
||||
self.telegram_pollers[project_name][bot_name] = new_process
|
||||
thread.start()
|
||||
self.telegram_pollers[project_name][bot_name] = (bot, thread)
|
||||
print(f'started process for {project_name} {bot_name}')
|
||||
else:
|
||||
if process is None or not process.is_alive:
|
||||
if internal_bot_info is None or not internal_bot_info[1].is_alive:
|
||||
print(f'process for {project_name} {bot_name} is not alive')
|
||||
continue
|
||||
print(f'terminating process for {project_name} {bot_name}')
|
||||
process.terminate()
|
||||
internal_bot_info[0].stop_bot()
|
||||
self.telegram_pollers[project_name][bot_name] = None
|
||||
print(f'terminated process for {project_name} {bot_name}')
|
||||
time.sleep(10)
|
||||
|
||||
def start_polling(self, telegram_token, queue):
|
||||
bot = telebot.TeleBot(telegram_token)
|
||||
def start_polling(self, bot, queue):
|
||||
@bot.message_handler()
|
||||
def do_action(message):
|
||||
queues.set_task(queue, message.json, 1)
|
||||
|
Loading…
Reference in New Issue
Block a user