Compare commits

..

9 Commits

Author SHA1 Message Date
c8f65a0ebb Merge pull request 'fix' (#9) from master into dev
Reviewed-on: #9
2024-11-27 11:10:47 +03:00
6401a40f11 Merge pull request 'fix' (#8) from master into dev
Reviewed-on: #8
2024-11-27 04:32:35 +03:00
32197fd699 Merge pull request 'fix' (#7) from master into dev
Reviewed-on: #7
2024-11-27 04:26:41 +03:00
e69ee8767a Merge pull request 'fix' (#6) from master into dev
Reviewed-on: #6
2024-11-27 04:19:26 +03:00
54f7581657 Merge pull request 'fix' (#5) from master into dev
Reviewed-on: #5
2024-11-27 04:14:58 +03:00
c6a2710087 Merge pull request 'fix' (#4) from master into dev
Reviewed-on: #4
2024-11-27 04:11:15 +03:00
42fc5552ab Merge pull request 'fix' (#3) from master into dev
Reviewed-on: #3
2024-11-27 04:09:16 +03:00
499eed49e0 Merge pull request 'fix' (#2) from master into dev
Reviewed-on: #2
2024-11-27 04:07:49 +03:00
349df7eb17 Merge pull request 'req' (#1) from master into dev
Reviewed-on: #1
2024-11-27 04:06:16 +03:00
8 changed files with 58 additions and 92 deletions

3
.gitignore vendored
View File

@@ -119,6 +119,3 @@ GitHub.sublime-settings
.history .history
local_platform.json local_platform.json
*pb2*
schemas

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", "main.py"] ENTRYPOINT ["python", "main.py"]

View File

@@ -1,5 +1,3 @@
import pydantic
from telebot import apihelper from telebot import apihelper
from daemons import base from daemons import base
@@ -7,13 +5,6 @@ from utils import platform
from utils import queues from utils import queues
class Message(pydantic.BaseModel):
project: str
name: str
body: dict
method: str = 'send_message'
class Daemon(base.Daemon, queues.TasksHandlerMixin): class Daemon(base.Daemon, queues.TasksHandlerMixin):
def execute(self): def execute(self):
self.poll() self.poll()
@@ -22,19 +13,16 @@ class Daemon(base.Daemon, queues.TasksHandlerMixin):
def queue_name(self): def queue_name(self):
return 'botalka_mailbox' return 'botalka_mailbox'
def process(self, payload: dict): def process(self, payload):
message = Message.model_validate(payload) bot = platform.platform_client.get_config('bots')[payload['project']][payload['name']]
bot = platform.platform_client.get_config('bots')[message.project][message.name]
if not bot['mailbox_enabled']: if not bot['mailbox_enabled']:
return return
if bot['type'] == 'telegram': if bot['type'] == 'telegram':
token = bot['secrets']['telegram_token'] token = bot['secrets']['telegram_token']
self.process_telegram(token, message.method, message.body) self.process_telegram(token, payload['body'])
else:
print('Unknown bot type:', bot['type'])
def process_telegram(self, token, method, payload): def process_telegram(self, token, payload):
try: try:
getattr(apihelper, method)(token, **payload) apihelper.send_message(token, **payload)
except Exception as exc: except Exception as exc:
print('Error', str(exc)) print('Error', str(exc))

View File

@@ -1,7 +1,6 @@
import telebot import telebot
import multiprocessing import threading
import time import time
import json
from daemons import base from daemons import base
from utils import platform from utils import platform
@@ -10,37 +9,42 @@ from utils import queues
class Daemon(base.Daemon): class Daemon(base.Daemon):
def __init__(self): def __init__(self):
self.processes: dict[str, multiprocessing.Process|None] = {} self.telegram_bots: dict[str, dict[str, tuple[telebot.TeleBot, 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_pollers:
self.telegram_pollers[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_pollers[project_name]:
proc = self.processes.get(key) self.telegram_pollers[project_name][bot_name] = None
internal_bot_info = self.telegram_pollers[project_name][bot_name]
if bot_info.get('poll_enabled'): if bot_info.get('poll_enabled'):
if proc and proc.is_alive(): 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') print(f'process for {project_name} {bot_name} is alive')
continue 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}') 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'])) thread.start()
process.start() self.telegram_pollers[project_name][bot_name] = (bot, thread)
self.processes[key] = process
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 internal_bot_info is None or not internal_bot_info[1].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}')
proc.terminate() internal_bot_info[0].stop_bot()
self.processes[key] = 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, token: str, queue: str): def start_polling(self, bot, queue):
bot = telebot.TeleBot(token) @bot.message_handler()
@bot.message_handler(content_types=['audio', 'photo', 'voice', 'video', 'document', 'animation', 'text', 'location', 'contact', 'sticker', 'video_note']) def do_action(message):
def do_action(message: telebot.types.Message):
queues.set_task(queue, message.json, 1) queues.set_task(queue, message.json, 1)
bot.polling() bot.polling()

19
local_platform.json Normal file
View File

@@ -0,0 +1,19 @@
{
"configs": {
"bots": {
"pizda-bot": {
"telegram-bot": {
"type": "telegram",
"poll_enabled": true,
"mailbox_enabled": false,
"queue": "pizda_bot_worker",
"secrets": {
"telegram_token": "5652624181:AAFFVjcHJjea7wlW6MRaxjFY_eu2XWPwOns"
}
}
}
}
},
"experiments": {},
"platform_staff": {}
}

View File

@@ -1,8 +1,8 @@
import sys import sys
arg = sys.argv[-1] # arg = sys.argv[-1]
# arg = 'poll' arg = 'poll'
if __name__ == '__main__': if __name__ == '__main__':
if arg == "poll": if arg == "poll":

View File

@@ -1,10 +1,6 @@
annotated-types==0.7.0 certifi==2024.8.30
certifi==2024.12.14
charset-normalizer==3.4.0 charset-normalizer==3.4.0
idna==3.10 idna==3.10
pydantic==2.10.4
pydantic_core==2.27.2
pyTelegramBotAPI==4.1.1 pyTelegramBotAPI==4.1.1
requests==2.32.3 requests==2.32.3
typing_extensions==4.12.2 urllib3==2.2.3
urllib3==2.3.0

View File

@@ -1,10 +1,4 @@
from concurrent.futures import ThreadPoolExecutor
import datetime
import json
import os import os
import traceback
import uuid
import zoneinfo
import requests import requests
import time import time
@@ -21,55 +15,24 @@ class QueuesException(Exception):
class TasksHandlerMixin: class TasksHandlerMixin:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.executor = ThreadPoolExecutor(max_workers=1)
def _send_metric(self, start: datetime.datetime, end: datetime.datetime, success: bool):
def send():
requests.post(f'{QUEUES_URL}/api/v1/metric', json={
'service': 'botalka',
'queue': self.queue_name,
'success': success,
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
"success": success,
"execution_time_ms": (end - start).microseconds // 1000,
"environment": stage,
})
self.executor.submit(send)
def poll(self): def poll(self):
while True: while True:
try:
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name}).json() response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name}).json()
except requests.JSONDecodeError:
print('Unable to decode json')
time.sleep(3)
continue
task = response.get('task') task = response.get('task')
if not task: if not task:
time.sleep(0.2) time.sleep(0.2)
continue continue
start = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
try: try:
print(f'process task with id {task["id"]}, attempt {task["attempt"]}')
self.process(task['payload']) self.process(task['payload'])
success = True
except Exception as exc: except Exception as exc:
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}') print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
traceback.print_stack() continue
success = False
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
if success:
try: try:
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']}) resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
if resp.status_code != 202: if resp.status_code != 202:
raise QueuesException raise QueuesException
print(f'finish task with id {task["id"]}')
except: except:
print(f'Failed to finish task id={task["id"]}') print(f'Failed to finish task id={task["id"]}')
self._send_metric(start, end, success)
@property @property
def queue_name(self): def queue_name(self):