Compare commits
No commits in common. "c8f65a0ebbe5e090431200c81dc953a3a1dad2ab" and "6401a40f11f36b376eea982c174baee5c6032d25" have entirely different histories.
c8f65a0ebb
...
6401a40f11
2
.gitignore
vendored
2
.gitignore
vendored
@ -117,5 +117,3 @@ GitHub.sublime-settings
|
|||||||
!.vscode/launch.json
|
!.vscode/launch.json
|
||||||
!.vscode/extensions.json
|
!.vscode/extensions.json
|
||||||
.history
|
.history
|
||||||
|
|
||||||
local_platform.json
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import telebot
|
import telebot
|
||||||
import threading
|
import multiprocessing
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from daemons import base
|
from daemons import base
|
||||||
@ -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_pollers: dict[str, dict[str, multiprocessing.Process|None]] = {}
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
while True:
|
while True:
|
||||||
@ -20,30 +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_pollers[project_name]:
|
if bot_name not in self.telegram_pollers[project_name]:
|
||||||
self.telegram_pollers[project_name][bot_name] = None
|
self.telegram_pollers[project_name][bot_name] = None
|
||||||
internal_bot_info = self.telegram_pollers[project_name][bot_name]
|
process = self.telegram_pollers[project_name][bot_name]
|
||||||
if bot_info.get('poll_enabled'):
|
if bot_info.get('poll_enabled'):
|
||||||
if internal_bot_info is not None:
|
if process is not None and process.is_alive:
|
||||||
bot, thread = internal_bot_info
|
|
||||||
if thread.is_alive:
|
|
||||||
print(f'process for {project_name} {bot_name} is alive')
|
print(f'process for {project_name} {bot_name} is alive')
|
||||||
continue
|
continue
|
||||||
bot = telebot.TeleBot(bot_info['secrets']['telegram_token'])
|
new_process = multiprocessing.Process(target=self.start_polling, args=[bot_info['secrets']['telegram_token'], bot_info['queue']])
|
||||||
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()
|
new_process.start()
|
||||||
self.telegram_pollers[project_name][bot_name] = (bot, thread)
|
self.telegram_pollers[project_name][bot_name] = new_process
|
||||||
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 process is None or not process.is_alive:
|
||||||
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()
|
process.terminate()
|
||||||
self.telegram_pollers[project_name][bot_name] = None
|
self.telegram_pollers[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, telegram_token, queue):
|
||||||
|
bot = telebot.TeleBot(telegram_token)
|
||||||
@bot.message_handler()
|
@bot.message_handler()
|
||||||
def do_action(message):
|
def do_action(message):
|
||||||
queues.set_task(queue, message.json, 1)
|
queues.set_task(queue, message.json, 1)
|
||||||
|
Loading…
Reference in New Issue
Block a user