Compare commits

..

No commits in common. "d3d92f56ee0211bc8ae7e00f35a3936f13a68dda" and "55f6582b006fd585f56e6445774d0685d8b8d30a" have entirely different histories.

8 changed files with 39 additions and 57 deletions

2
.gitignore vendored
View File

@ -117,5 +117,3 @@ GitHub.sublime-settings
!.vscode/launch.json !.vscode/launch.json
!.vscode/extensions.json !.vscode/extensions.json
.history .history
local_platform.json

View File

@ -4,5 +4,4 @@ WORKDIR /usr/src/app
COPY requirements.txt requirements.txt COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
COPY . . COPY . .
ENV PYTHONUNBUFFERED 1 ENTRYPOINT ["python", "entrypoint.py"]
ENTRYPOINT ["python", "main.py"]

View File

@ -1,3 +1,3 @@
class Daemon: class Base:
def execute(self): def execute(self):
raise NotImplemented raise NotImplemented

View File

@ -20,8 +20,6 @@ class Daemon(base.Daemon, queues.TasksHandlerMixin):
if bot['type'] == 'telegram': if bot['type'] == 'telegram':
token = bot['secrets']['telegram_token'] token = bot['secrets']['telegram_token']
self.process_telegram(token, payload['body']) self.process_telegram(token, payload['body'])
else:
print('Unknown bot type:', bot['type'])
def process_telegram(self, token, payload): def process_telegram(self, token, payload):
try: try:

View File

@ -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,39 +9,34 @@ 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, telebot.TeleBot|None]] = {} self.telegram_pollers: dict[str, dict[str, multiprocessing.Process|None]] = {}
def execute(self): def execute(self):
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: if project_name not in self.telegram_pollers:
self.telegram_bots[project_name] = {} self.telegram_pollers[project_name] = {}
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_pollers[project_name]:
self.telegram_bots[project_name][bot_name] = None self.telegram_pollers[project_name][bot_name] = None
bot = self.telegram_bots[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 bot is not None: if process is not None and process.is_alive:
print(f'process for {project_name} {bot_name} is alive')
continue continue
print(f'starting process for {project_name} {bot_name}') new_process = multiprocessing.Process(target=self.start_polling, args=[bot_info['secrets']['telegram_token'], bot_info['queue']])
bot = telebot.TeleBot(bot_info['secrets']['telegram_token']) new_process.start()
self.start_polling(bot, bot_info['queue']) self.telegram_pollers[project_name][bot_name] = new_process
self.telegram_bots[project_name][bot_name] = bot
print(f'started process for {project_name} {bot_name}')
else: else:
if bot is None: if process is None:
print(f'process for {project_name} {bot_name} is not alive')
continue continue
print(f'terminating process for {project_name} {bot_name}') if process.is_alive:
bot.stop_bot() process.terminate()
self.telegram_bots[project_name][bot_name] = None self.telegram_pollers[project_name][bot_name] = None
print(f'terminated process for {project_name} {bot_name}')
time.sleep(10) time.sleep(10)
def start_polling(self, bot: telebot.TeleBot, queue: str): def start_polling(telegram_token, queue):
bot = telebot.TeleBot(telegram_token)
@bot.message_handler() @bot.message_handler()
def do_action(message: telebot.types.Message): def do_action(message):
queues.set_task(queue, message.json, 1) queues.set_task(queue, message.json, 1)
threading.Thread(target=bot.polling).start() bot.polling()

10
main.py
View File

@ -2,16 +2,14 @@ import sys
arg = sys.argv[-1] arg = sys.argv[-1]
# arg = 'poll'
if __name__ == '__main__': if arg == "poll":
if arg == "poll":
print("poll is starting") print("poll is starting")
from daemons.poll import Daemon from daemons.poll import Daemon
elif arg == 'mailbox': elif arg == 'mailbox':
print("mailbox is starting") print("mailbox is starting")
from daemons.mailbox import Daemon from daemons.mailbox import Daemon
else: else:
raise ValueError(f"Unknown param {arg}") raise ValueError(f"Unknown param {arg}")
Daemon().execute() Daemon().execute()

View File

@ -1,6 +0,0 @@
certifi==2024.8.30
charset-normalizer==3.4.0
idna==3.10
pyTelegramBotAPI==4.1.1
requests==2.32.3
urllib3==2.2.3

View File

@ -88,6 +88,6 @@ class PlatformClient:
platform_client = PlatformClient( platform_client = PlatformClient(
'Botalka', 'Botalka',
os.getenv('STAGE', 'local'), os.getenv('STAGE'),
need_poll=True, need_poll=True,
) )