Compare commits
35 Commits
32cdf983e8
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| d5895b8888 | |||
|
|
8799b3107b | ||
| 83718675d7 | |||
|
|
7a2950e480 | ||
| 50a26e0e1d | |||
|
|
7448b4e0c6 | ||
| d670c9375c | |||
| 5e2c725bc2 | |||
| 11fbbf18f4 | |||
| 595dfd0f6d | |||
| 0990f684da | |||
|
|
5fa5789559 | ||
| 232042a523 | |||
|
|
7fcbb316e6 | ||
| eec041bb9d | |||
|
|
e9ecb7d94f | ||
| e8b5092745 | |||
|
|
dbe5df157b | ||
| 6c1288aaab | |||
|
|
f4af02ec1d | ||
| 3408364bc4 | |||
| baa134ae26 | |||
| fcb2713eaf | |||
| d2247054e8 | |||
| ce550c31f4 | |||
| ba2f1bb768 | |||
| 889a20609f | |||
| 1c3d1566e5 | |||
| 2b88479cec | |||
| 7238d725dd | |||
| 4a3fa3c2ab | |||
| 45e1c0fe8a | |||
| 989f7026ad | |||
| 90378be7c5 | |||
| 95e4f5e8da |
@@ -6,12 +6,12 @@ services:
|
||||
worker:
|
||||
image: mathwave/sprint-repo:ruz-bot
|
||||
environment:
|
||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||
STAGE: "development"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||
networks:
|
||||
- queues-development
|
||||
- configurator
|
||||
- mongo-development
|
||||
command: worker
|
||||
deploy:
|
||||
mode: replicated
|
||||
@@ -24,12 +24,11 @@ services:
|
||||
fetch:
|
||||
image: mathwave/sprint-repo:ruz-bot
|
||||
environment:
|
||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||
STAGE: "development"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||
networks:
|
||||
- queues-development
|
||||
- mongo-development
|
||||
command: fetch
|
||||
deploy:
|
||||
mode: replicated
|
||||
@@ -42,12 +41,11 @@ services:
|
||||
notify:
|
||||
image: mathwave/sprint-repo:ruz-bot
|
||||
environment:
|
||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||
STAGE: "development"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||
networks:
|
||||
- queues-development
|
||||
- mongo-development
|
||||
command: notify
|
||||
deploy:
|
||||
mode: replicated
|
||||
@@ -60,14 +58,12 @@ services:
|
||||
ruz-bot-nginx:
|
||||
image: mathwave/sprint-repo:ruz-bot
|
||||
networks:
|
||||
- common-infra-nginx
|
||||
- common-infra-nginx-development
|
||||
- queues-development
|
||||
- mongo-development
|
||||
environment:
|
||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||
STAGE: "development"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||
networks:
|
||||
- queues-development
|
||||
command: api
|
||||
deploy:
|
||||
mode: replicated
|
||||
@@ -84,3 +80,5 @@ networks:
|
||||
external: true
|
||||
configurator:
|
||||
external: true
|
||||
mongo-development:
|
||||
external: true
|
||||
|
||||
@@ -6,12 +6,12 @@ services:
|
||||
worker:
|
||||
image: mathwave/sprint-repo:ruz-bot
|
||||
environment:
|
||||
MONGO_HOST: "mongo.sprinthub.ru"
|
||||
STAGE: "production"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||
networks:
|
||||
- queues
|
||||
- configurator
|
||||
- mongo
|
||||
command: worker
|
||||
deploy:
|
||||
mode: replicated
|
||||
@@ -24,13 +24,13 @@ services:
|
||||
fetch:
|
||||
image: mathwave/sprint-repo:ruz-bot
|
||||
environment:
|
||||
MONGO_HOST: "mongo.sprinthub.ru"
|
||||
STAGE: "production"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||
DEBUG: "false"
|
||||
networks:
|
||||
- queues
|
||||
- configurator
|
||||
- mongo
|
||||
command: fetch
|
||||
deploy:
|
||||
mode: replicated
|
||||
@@ -43,13 +43,13 @@ services:
|
||||
notify:
|
||||
image: mathwave/sprint-repo:ruz-bot
|
||||
environment:
|
||||
MONGO_HOST: "mongo.sprinthub.ru"
|
||||
STAGE: "production"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||
DEBUG: "false"
|
||||
networks:
|
||||
- queues
|
||||
- configurator
|
||||
- mongo
|
||||
command: notify
|
||||
deploy:
|
||||
mode: replicated
|
||||
@@ -64,8 +64,8 @@ services:
|
||||
networks:
|
||||
- common-infra-nginx
|
||||
- configurator
|
||||
- mongo
|
||||
environment:
|
||||
MONGO_HOST: "mongo.sprinthub.ru"
|
||||
STAGE: "production"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||
DEBUG: "false"
|
||||
@@ -85,3 +85,5 @@ networks:
|
||||
external: true
|
||||
configurator:
|
||||
external: true
|
||||
mongo:
|
||||
external: true
|
||||
|
||||
@@ -7,6 +7,7 @@ RUN apt-get install -y locales locales-all
|
||||
ENV LANGUAGE ru_RU.UTF-8
|
||||
ENV LANG ru_RU.UTF-8
|
||||
ENV LC_ALL ru_RU.UTF-8
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
COPY requirements.txt requirements.txt
|
||||
RUN pip install -r requirements.txt
|
||||
COPY . .
|
||||
|
||||
@@ -55,13 +55,6 @@ class Answer:
|
||||
|
||||
def process(self):
|
||||
user = User(self.user['chat_id'])
|
||||
try:
|
||||
bot_enabled_exp = platform.get_experiment('bot_enabled')
|
||||
if not bot_enabled_exp['enabled'] or not eval(bot_enabled_exp['condition']):
|
||||
return
|
||||
except Exception as exc:
|
||||
logging.info(exc)
|
||||
return
|
||||
getattr(
|
||||
self,
|
||||
"handle_state_" + self.user['state'],
|
||||
@@ -77,7 +70,7 @@ class Answer:
|
||||
body = {'text': text, 'chat_id': self.user['chat_id'], 'parse_mode': 'Markdown'}
|
||||
if reply_markup:
|
||||
body['reply_markup'] = reply_markup.to_json()
|
||||
queues.set_task('botalka_mailbox', {'project': 'ruz-bot', 'name': 'telegram-bot', 'body': body}, 1)
|
||||
queues.set_task('botalka_mailbox', {'project': 'ruz-bot', 'name': 'telegram-bot', 'body': body}, 5)
|
||||
|
||||
def set_state(self, state: str):
|
||||
self.user['state'] = state
|
||||
|
||||
@@ -9,7 +9,7 @@ from helpers import now
|
||||
|
||||
class Mongo:
|
||||
def __init__(self):
|
||||
url = f"mongodb://{settings.MONGO_USER}:{settings.MONGO_PASSWORD}@{settings.MONGO_HOST}:27017/"
|
||||
url = f"mongodb://{settings.MONGO_USER}:{settings.MONGO_PASSWORD}@mongo:27017/"
|
||||
self.client = pymongo.MongoClient(url)
|
||||
self.database = self.client.get_database("ruz-bot")
|
||||
self.users_collection.create_index([
|
||||
|
||||
@@ -12,7 +12,8 @@ fields = [
|
||||
'date_start',
|
||||
'date_end',
|
||||
'lecturer_profiles',
|
||||
'stream_links'
|
||||
'stream_links',
|
||||
'type',
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import datetime
|
||||
import os
|
||||
import traceback
|
||||
import zoneinfo
|
||||
import requests
|
||||
import time
|
||||
|
||||
@@ -15,24 +19,55 @@ class QueuesException(Exception):
|
||||
|
||||
|
||||
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': 'ruz-bot',
|
||||
'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):
|
||||
while True:
|
||||
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name}).json()
|
||||
try:
|
||||
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')
|
||||
if not task:
|
||||
time.sleep(0.2)
|
||||
continue
|
||||
start = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
try:
|
||||
print(f'process task with id {task["id"]}, attempt {task["attempt"]}')
|
||||
self.process(task['payload'])
|
||||
success = True
|
||||
except Exception as exc:
|
||||
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
|
||||
continue
|
||||
try:
|
||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||
if resp.status_code != 202:
|
||||
raise QueuesException
|
||||
except:
|
||||
print(f'Failed to finish task id={task["id"]}')
|
||||
traceback.print_exc()
|
||||
success = False
|
||||
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
if success:
|
||||
try:
|
||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||
if resp.status_code != 202:
|
||||
raise QueuesException
|
||||
print(f'finish task with id {task["id"]}')
|
||||
except:
|
||||
print(f'Failed to finish task id={task["id"]}')
|
||||
self._send_metric(start, end, success)
|
||||
|
||||
@property
|
||||
def queue_name(self):
|
||||
|
||||
Reference in New Issue
Block a user