Compare commits
7 Commits
master
...
32cdf983e8
| Author | SHA1 | Date | |
|---|---|---|---|
| 32cdf983e8 | |||
| b9aafe8c4f | |||
| b150f2fafc | |||
| f8ec8af981 | |||
| 26af411bf4 | |||
| 034f19aea9 | |||
| 8a10b1d180 |
@@ -6,12 +6,12 @@ services:
|
|||||||
worker:
|
worker:
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
image: mathwave/sprint-repo:ruz-bot
|
||||||
environment:
|
environment:
|
||||||
|
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
networks:
|
networks:
|
||||||
- queues-development
|
- queues-development
|
||||||
- configurator
|
- configurator
|
||||||
- mongo-development
|
|
||||||
command: worker
|
command: worker
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -24,11 +24,12 @@ services:
|
|||||||
fetch:
|
fetch:
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
image: mathwave/sprint-repo:ruz-bot
|
||||||
environment:
|
environment:
|
||||||
|
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
|
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||||
networks:
|
networks:
|
||||||
- queues-development
|
- queues-development
|
||||||
- mongo-development
|
|
||||||
command: fetch
|
command: fetch
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -41,11 +42,12 @@ services:
|
|||||||
notify:
|
notify:
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
image: mathwave/sprint-repo:ruz-bot
|
||||||
environment:
|
environment:
|
||||||
|
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
|
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||||
networks:
|
networks:
|
||||||
- queues-development
|
- queues-development
|
||||||
- mongo-development
|
|
||||||
command: notify
|
command: notify
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -58,12 +60,14 @@ services:
|
|||||||
ruz-bot-nginx:
|
ruz-bot-nginx:
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
image: mathwave/sprint-repo:ruz-bot
|
||||||
networks:
|
networks:
|
||||||
- common-infra-nginx-development
|
- common-infra-nginx
|
||||||
- queues-development
|
|
||||||
- mongo-development
|
|
||||||
environment:
|
environment:
|
||||||
|
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
|
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||||
|
networks:
|
||||||
|
- queues-development
|
||||||
command: api
|
command: api
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -80,5 +84,3 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
configurator:
|
configurator:
|
||||||
external: true
|
external: true
|
||||||
mongo-development:
|
|
||||||
external: true
|
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ services:
|
|||||||
worker:
|
worker:
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
image: mathwave/sprint-repo:ruz-bot
|
||||||
environment:
|
environment:
|
||||||
|
MONGO_HOST: "mongo.sprinthub.ru"
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
networks:
|
networks:
|
||||||
- queues
|
- queues
|
||||||
- configurator
|
- configurator
|
||||||
- mongo
|
|
||||||
command: worker
|
command: worker
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -24,13 +24,13 @@ services:
|
|||||||
fetch:
|
fetch:
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
image: mathwave/sprint-repo:ruz-bot
|
||||||
environment:
|
environment:
|
||||||
|
MONGO_HOST: "mongo.sprinthub.ru"
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
DEBUG: "false"
|
DEBUG: "false"
|
||||||
networks:
|
networks:
|
||||||
- queues
|
- queues
|
||||||
- configurator
|
- configurator
|
||||||
- mongo
|
|
||||||
command: fetch
|
command: fetch
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -43,13 +43,13 @@ services:
|
|||||||
notify:
|
notify:
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
image: mathwave/sprint-repo:ruz-bot
|
||||||
environment:
|
environment:
|
||||||
|
MONGO_HOST: "mongo.sprinthub.ru"
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
DEBUG: "false"
|
DEBUG: "false"
|
||||||
networks:
|
networks:
|
||||||
- queues
|
- queues
|
||||||
- configurator
|
- configurator
|
||||||
- mongo
|
|
||||||
command: notify
|
command: notify
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -64,8 +64,8 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- common-infra-nginx
|
- common-infra-nginx
|
||||||
- configurator
|
- configurator
|
||||||
- mongo
|
|
||||||
environment:
|
environment:
|
||||||
|
MONGO_HOST: "mongo.sprinthub.ru"
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
DEBUG: "false"
|
DEBUG: "false"
|
||||||
@@ -85,5 +85,3 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
configurator:
|
configurator:
|
||||||
external: true
|
external: true
|
||||||
mongo:
|
|
||||||
external: true
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ RUN apt-get install -y locales locales-all
|
|||||||
ENV LANGUAGE ru_RU.UTF-8
|
ENV LANGUAGE ru_RU.UTF-8
|
||||||
ENV LANG ru_RU.UTF-8
|
ENV LANG ru_RU.UTF-8
|
||||||
ENV LC_ALL ru_RU.UTF-8
|
ENV LC_ALL ru_RU.UTF-8
|
||||||
ENV PYTHONUNBUFFERED=1
|
|
||||||
COPY requirements.txt requirements.txt
|
COPY requirements.txt requirements.txt
|
||||||
RUN pip install -r requirements.txt
|
RUN pip install -r requirements.txt
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|||||||
@@ -55,6 +55,13 @@ class Answer:
|
|||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
user = User(self.user['chat_id'])
|
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(
|
getattr(
|
||||||
self,
|
self,
|
||||||
"handle_state_" + self.user['state'],
|
"handle_state_" + self.user['state'],
|
||||||
@@ -70,7 +77,7 @@ class Answer:
|
|||||||
body = {'text': text, 'chat_id': self.user['chat_id'], 'parse_mode': 'Markdown'}
|
body = {'text': text, 'chat_id': self.user['chat_id'], 'parse_mode': 'Markdown'}
|
||||||
if reply_markup:
|
if reply_markup:
|
||||||
body['reply_markup'] = reply_markup.to_json()
|
body['reply_markup'] = reply_markup.to_json()
|
||||||
queues.set_task('botalka_mailbox', {'project': 'ruz-bot', 'name': 'telegram-bot', 'body': body}, 5)
|
queues.set_task('botalka_mailbox', {'project': 'ruz-bot', 'name': 'telegram-bot', 'body': body}, 1)
|
||||||
|
|
||||||
def set_state(self, state: str):
|
def set_state(self, state: str):
|
||||||
self.user['state'] = state
|
self.user['state'] = state
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from helpers import now
|
|||||||
|
|
||||||
class Mongo:
|
class Mongo:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
url = f"mongodb://{settings.MONGO_USER}:{settings.MONGO_PASSWORD}@mongo:27017/"
|
url = f"mongodb://{settings.MONGO_USER}:{settings.MONGO_PASSWORD}@{settings.MONGO_HOST}:27017/"
|
||||||
self.client = pymongo.MongoClient(url)
|
self.client = pymongo.MongoClient(url)
|
||||||
self.database = self.client.get_database("ruz-bot")
|
self.database = self.client.get_database("ruz-bot")
|
||||||
self.users_collection.create_index([
|
self.users_collection.create_index([
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ fields = [
|
|||||||
'date_start',
|
'date_start',
|
||||||
'date_end',
|
'date_end',
|
||||||
'lecturer_profiles',
|
'lecturer_profiles',
|
||||||
'stream_links',
|
'stream_links'
|
||||||
'type',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
from concurrent.futures import ThreadPoolExecutor
|
|
||||||
import datetime
|
|
||||||
import os
|
import os
|
||||||
import traceback
|
|
||||||
import zoneinfo
|
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -19,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': '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):
|
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_exc()
|
continue
|
||||||
success = False
|
try:
|
||||||
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||||
if success:
|
if resp.status_code != 202:
|
||||||
try:
|
raise QueuesException
|
||||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
except:
|
||||||
if resp.status_code != 202:
|
print(f'Failed to finish task id={task["id"]}')
|
||||||
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
|
@property
|
||||||
def queue_name(self):
|
def queue_name(self):
|
||||||
@@ -84,4 +49,4 @@ def set_task(queue_name: str, payload: dict, seconds_to_execute: int, delay: int
|
|||||||
'delay': delay,
|
'delay': delay,
|
||||||
})
|
})
|
||||||
if resp.status_code != 202:
|
if resp.status_code != 202:
|
||||||
raise QueuesException
|
raise QueuesException
|
||||||
|
|||||||
Reference in New Issue
Block a user