Compare commits
41 Commits
queues
...
d5895b8888
| Author | SHA1 | Date | |
|---|---|---|---|
| d5895b8888 | |||
|
|
8799b3107b | ||
| 83718675d7 | |||
|
|
7a2950e480 | ||
| 50a26e0e1d | |||
|
|
7448b4e0c6 | ||
| d670c9375c | |||
| 5e2c725bc2 | |||
| 11fbbf18f4 | |||
| 595dfd0f6d | |||
| 0990f684da | |||
|
|
5fa5789559 | ||
| 232042a523 | |||
|
|
7fcbb316e6 | ||
| eec041bb9d | |||
|
|
e9ecb7d94f | ||
| e8b5092745 | |||
|
|
dbe5df157b | ||
| 6c1288aaab | |||
|
|
f4af02ec1d | ||
| 156e1407ed | |||
| 3408364bc4 | |||
| 18aabe497b | |||
| baa134ae26 | |||
| 144e5d4335 | |||
| fcb2713eaf | |||
| 48ab0d1797 | |||
| ddfd5596d9 | |||
| f8417edc94 | |||
| d2247054e8 | |||
| ce550c31f4 | |||
| ba2f1bb768 | |||
| 889a20609f | |||
| 1c3d1566e5 | |||
| 2b88479cec | |||
| 7238d725dd | |||
| 4a3fa3c2ab | |||
| 45e1c0fe8a | |||
| 989f7026ad | |||
| 90378be7c5 | |||
| 95e4f5e8da |
@@ -3,32 +3,15 @@ version: "3.4"
|
|||||||
|
|
||||||
services:
|
services:
|
||||||
|
|
||||||
poll:
|
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
|
||||||
environment:
|
|
||||||
STAGE: "development"
|
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
|
||||||
networks:
|
|
||||||
- queues-development
|
|
||||||
command: poll
|
|
||||||
deploy:
|
|
||||||
mode: replicated
|
|
||||||
restart_policy:
|
|
||||||
condition: any
|
|
||||||
update_config:
|
|
||||||
parallelism: 1
|
|
||||||
order: start-first
|
|
||||||
|
|
||||||
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
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
networks:
|
networks:
|
||||||
- queues-development
|
- queues-development
|
||||||
- configurator
|
- configurator
|
||||||
|
- mongo-development
|
||||||
command: worker
|
command: worker
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -38,31 +21,14 @@ services:
|
|||||||
parallelism: 1
|
parallelism: 1
|
||||||
order: start-first
|
order: start-first
|
||||||
|
|
||||||
mailbox:
|
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
|
||||||
environment:
|
|
||||||
STAGE: "development"
|
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
|
||||||
networks:
|
|
||||||
- queues-development
|
|
||||||
command: mailbox
|
|
||||||
deploy:
|
|
||||||
mode: replicated
|
|
||||||
restart_policy:
|
|
||||||
condition: any
|
|
||||||
update_config:
|
|
||||||
parallelism: 1
|
|
||||||
order: start-first
|
|
||||||
|
|
||||||
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
|
||||||
@@ -75,12 +41,11 @@ 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
|
||||||
@@ -93,14 +58,12 @@ services:
|
|||||||
ruz-bot-nginx:
|
ruz-bot-nginx:
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
image: mathwave/sprint-repo:ruz-bot
|
||||||
networks:
|
networks:
|
||||||
- common-infra-nginx
|
- common-infra-nginx-development
|
||||||
|
- 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
|
||||||
@@ -117,3 +80,5 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
configurator:
|
configurator:
|
||||||
external: true
|
external: true
|
||||||
|
mongo-development:
|
||||||
|
external: true
|
||||||
|
|||||||
@@ -3,32 +3,15 @@ version: "3.4"
|
|||||||
|
|
||||||
services:
|
services:
|
||||||
|
|
||||||
poll:
|
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
|
||||||
environment:
|
|
||||||
STAGE: "production"
|
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
|
||||||
networks:
|
|
||||||
- queues
|
|
||||||
command: poll
|
|
||||||
deploy:
|
|
||||||
mode: replicated
|
|
||||||
restart_policy:
|
|
||||||
condition: any
|
|
||||||
update_config:
|
|
||||||
parallelism: 1
|
|
||||||
order: start-first
|
|
||||||
|
|
||||||
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
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
networks:
|
networks:
|
||||||
- queues
|
- queues
|
||||||
- configurator
|
- configurator
|
||||||
|
- mongo
|
||||||
command: worker
|
command: worker
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -38,41 +21,21 @@ services:
|
|||||||
parallelism: 1
|
parallelism: 1
|
||||||
order: start-first
|
order: start-first
|
||||||
|
|
||||||
mailbox:
|
|
||||||
image: mathwave/sprint-repo:ruz-bot
|
|
||||||
environment:
|
|
||||||
STAGE: "production"
|
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
|
||||||
networks:
|
|
||||||
- queues
|
|
||||||
command: mailbox
|
|
||||||
deploy:
|
|
||||||
mode: replicated
|
|
||||||
restart_policy:
|
|
||||||
condition: any
|
|
||||||
update_config:
|
|
||||||
parallelism: 1
|
|
||||||
order: start-first
|
|
||||||
|
|
||||||
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
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
DEBUG: "false"
|
DEBUG: "false"
|
||||||
networks:
|
networks:
|
||||||
- queues
|
- queues
|
||||||
|
- configurator
|
||||||
|
- mongo
|
||||||
command: fetch
|
command: fetch
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
condition: any
|
condition: any
|
||||||
placement:
|
|
||||||
constraints:
|
|
||||||
- node.role == worker
|
|
||||||
- node.labels.zone == ru
|
|
||||||
update_config:
|
update_config:
|
||||||
parallelism: 1
|
parallelism: 1
|
||||||
order: start-first
|
order: start-first
|
||||||
@@ -80,22 +43,18 @@ 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
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
DEBUG: "false"
|
DEBUG: "false"
|
||||||
networks:
|
networks:
|
||||||
- queues
|
- queues
|
||||||
|
- configurator
|
||||||
|
- mongo
|
||||||
command: notify
|
command: notify
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
condition: any
|
condition: any
|
||||||
placement:
|
|
||||||
constraints:
|
|
||||||
- node.role == worker
|
|
||||||
- node.labels.zone == ru
|
|
||||||
update_config:
|
update_config:
|
||||||
parallelism: 1
|
parallelism: 1
|
||||||
order: start-first
|
order: start-first
|
||||||
@@ -104,21 +63,17 @@ services:
|
|||||||
image: mathwave/sprint-repo:ruz-bot
|
image: mathwave/sprint-repo:ruz-bot
|
||||||
networks:
|
networks:
|
||||||
- common-infra-nginx
|
- common-infra-nginx
|
||||||
|
- configurator
|
||||||
|
- mongo
|
||||||
environment:
|
environment:
|
||||||
MONGO_HOST: "mongo.sprinthub.ru"
|
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
DEBUG: "false"
|
DEBUG: "false"
|
||||||
command: api
|
command: api
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
condition: any
|
condition: any
|
||||||
placement:
|
|
||||||
constraints:
|
|
||||||
- node.role == worker
|
|
||||||
- node.labels.zone == ru
|
|
||||||
update_config:
|
update_config:
|
||||||
parallelism: 1
|
parallelism: 1
|
||||||
order: start-first
|
order: start-first
|
||||||
@@ -130,3 +85,5 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
configurator:
|
configurator:
|
||||||
external: true
|
external: true
|
||||||
|
mongo:
|
||||||
|
external: true
|
||||||
|
|||||||
@@ -41,5 +41,4 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
TELEGRAM_TOKEN_DEV: ${{ secrets.TELEGRAM_TOKEN_DEV }}
|
TELEGRAM_TOKEN_DEV: ${{ secrets.TELEGRAM_TOKEN_DEV }}
|
||||||
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
|
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
|
||||||
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
|
|
||||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml ruz-bot-development
|
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml ruz-bot-development
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ 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 . .
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
import telebot
|
|
||||||
import os
|
|
||||||
|
|
||||||
from daemons import base
|
|
||||||
from utils import queues
|
|
||||||
|
|
||||||
|
|
||||||
class Daemon(base.Daemon, queues.TasksHandlerMixin):
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__()
|
|
||||||
self.bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
|
||||||
|
|
||||||
@property
|
|
||||||
def queue_name(self):
|
|
||||||
return 'ruz_bot_mailbox'
|
|
||||||
|
|
||||||
def execute(self):
|
|
||||||
self.poll()
|
|
||||||
|
|
||||||
def process(self, payload):
|
|
||||||
body = {
|
|
||||||
'chat_id': payload['chat_id'],
|
|
||||||
'text': payload['text'],
|
|
||||||
}
|
|
||||||
reply_markup = payload.get('reply_markup')
|
|
||||||
if reply_markup:
|
|
||||||
body['reply_markup'] = reply_markup
|
|
||||||
try:
|
|
||||||
self.bot.send_message(**body, parse_mode='Markdown')
|
|
||||||
except Exception as exc:
|
|
||||||
print('Error', str(exc))
|
|
||||||
@@ -25,7 +25,7 @@ def process():
|
|||||||
ans += f"🧑🏫 {(lesson['lecturer'] or 'Неизвестно')}\n"
|
ans += f"🧑🏫 {(lesson['lecturer'] or 'Неизвестно')}\n"
|
||||||
if lesson.get('link', None):
|
if lesson.get('link', None):
|
||||||
ans += f"🔗 {lesson['link']}"
|
ans += f"🔗 {lesson['link']}"
|
||||||
queues.set_task('ruz_bot_mailbox', {'text': f"Через {user['notify_minutes']} минут у тебя занятие!\n" + ans, 'chat_id': user["chat_id"]}, 1)
|
queues.set_task('botalka_mailbox', {'project': 'ruz-bot', 'name': 'telegram-bot', 'body': {'text': f"Через {user['notify_minutes']} минут у тебя занятие!\n" + ans, 'chat_id': user["chat_id"], 'parse_mode': 'Markdown'}}, 1)
|
||||||
mongo.lessons_collection.update_one({"_id": lesson['_id']}, {"$set": {"notified": True}})
|
mongo.lessons_collection.update_one({"_id": lesson['_id']}, {"$set": {"notified": True}})
|
||||||
time_now = datetime.datetime.now()
|
time_now = datetime.datetime.now()
|
||||||
for user in mongo.users_collection.find({"next_daily_notify_time": {"$lte": time_now}}):
|
for user in mongo.users_collection.find({"next_daily_notify_time": {"$lte": time_now}}):
|
||||||
@@ -40,7 +40,7 @@ def process():
|
|||||||
else:
|
else:
|
||||||
text = ruz.schedule_builder(lessons)
|
text = ruz.schedule_builder(lessons)
|
||||||
try:
|
try:
|
||||||
queues.set_task('ruz_bot_mailbox', {'text': f"Уведомляю о занятиях! Твое расписание на {'сегодня' if user.get('daily_notify_today', True) else 'завтра'}:\n" + text, 'chat_id': user["chat_id"]}, 1)
|
queues.set_task('botalka_mailbox', {'project': 'ruz-bot', 'name': 'telegram-bot', 'body': {'text': f"Уведомляю о занятиях! Твое расписание на {'сегодня' if user.get('daily_notify_today', True) else 'завтра'}:\n" + text, 'chat_id': user["chat_id"], 'parse_mode': 'Markdown'}}, 1)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
mongo.users_collection.update_one(
|
mongo.users_collection.update_one(
|
||||||
@@ -72,7 +72,7 @@ def process():
|
|||||||
else:
|
else:
|
||||||
mess += "12 часов"
|
mess += "12 часов"
|
||||||
mess += "!\n\nТвоя первая пара:\n\n" + ans
|
mess += "!\n\nТвоя первая пара:\n\n" + ans
|
||||||
queues.set_task('ruz_bot_mailbox', {'text': mess, 'chat_id': user["chat_id"]}, 1)
|
queues.set_task('botalka_mailbox', {'project': 'ruz-bot', 'name': 'telegram-bot', 'body': {'text': mess, 'chat_id': user["chat_id"], 'parse_mode': 'Markdown'}}, 1)
|
||||||
start_of_day = datetime.datetime(year=time_now.year, month=time_now.month, day=time_now.day)
|
start_of_day = datetime.datetime(year=time_now.year, month=time_now.month, day=time_now.day)
|
||||||
mongo.lessons_collection.update_many({"begin": {"$gte": start_of_day, "$lt": (start_of_day + datetime.timedelta(days=1))}, "user_email": user["email"]}, {"$set": {"notified_today": True}})
|
mongo.lessons_collection.update_many({"begin": {"$gte": start_of_day, "$lt": (start_of_day + datetime.timedelta(days=1))}, "user_email": user["email"]}, {"$set": {"notified_today": True}})
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
import os
|
|
||||||
import telebot
|
|
||||||
|
|
||||||
from daemons import base
|
|
||||||
from telebot import types
|
|
||||||
from utils import queues
|
|
||||||
|
|
||||||
|
|
||||||
class Daemon(base.Daemon):
|
|
||||||
def execute(self):
|
|
||||||
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
|
||||||
@bot.message_handler()
|
|
||||||
def do_action(message: types.Message):
|
|
||||||
queues.set_task('ruz_bot_worker', message.json, 1)
|
|
||||||
bot.polling()
|
|
||||||
@@ -55,13 +55,6 @@ 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'],
|
||||||
@@ -74,10 +67,10 @@ class Answer:
|
|||||||
def send_message(self, text, reply_markup=None, remove_keyboard=True, **kwargs):
|
def send_message(self, text, reply_markup=None, remove_keyboard=True, **kwargs):
|
||||||
if reply_markup is None and remove_keyboard:
|
if reply_markup is None and remove_keyboard:
|
||||||
reply_markup = ReplyKeyboardRemove()
|
reply_markup = ReplyKeyboardRemove()
|
||||||
body = {'text': text, 'chat_id': self.user['chat_id']}
|
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('ruz_bot_mailbox', body, 1)
|
queues.set_task('botalka_mailbox', {'project': 'ruz-bot', 'name': 'telegram-bot', 'body': body}, 5)
|
||||||
|
|
||||||
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}@{settings.MONGO_HOST}:27017/"
|
url = f"mongodb://{settings.MONGO_USER}:{settings.MONGO_PASSWORD}@mongo: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,7 +12,8 @@ fields = [
|
|||||||
'date_start',
|
'date_start',
|
||||||
'date_end',
|
'date_end',
|
||||||
'lecturer_profiles',
|
'lecturer_profiles',
|
||||||
'stream_links'
|
'stream_links',
|
||||||
|
'type',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
import traceback
|
||||||
|
import zoneinfo
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -15,24 +19,55 @@ 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:
|
||||||
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')
|
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}')
|
||||||
continue
|
traceback.print_exc()
|
||||||
try:
|
success = False
|
||||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||||
if resp.status_code != 202:
|
if success:
|
||||||
raise QueuesException
|
try:
|
||||||
except:
|
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||||
print(f'Failed to finish task 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
|
@property
|
||||||
def queue_name(self):
|
def queue_name(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user