Compare commits
12 Commits
90378be7c5
...
queues
| Author | SHA1 | Date | |
|---|---|---|---|
| 292bacc963 | |||
| e76cfc7994 | |||
| c146fc7f1e | |||
| 2425ee2988 | |||
| bc4f74e20f | |||
| 4e5d483208 | |||
| 1d00a27b15 | |||
| c3886433ac | |||
| ac1c8745dc | |||
| 17c9b1b693 | |||
| e286a3078e | |||
| 64e957af2f |
@@ -8,7 +8,8 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
|
networks:
|
||||||
|
- queues-development
|
||||||
command: poll
|
command: poll
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -25,7 +26,9 @@ services:
|
|||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
|
networks:
|
||||||
|
- queues-development
|
||||||
|
- configurator
|
||||||
command: worker
|
command: worker
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -40,7 +43,8 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
|
networks:
|
||||||
|
- queues-development
|
||||||
command: mailbox
|
command: mailbox
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -57,7 +61,8 @@ services:
|
|||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
|
networks:
|
||||||
|
- queues-development
|
||||||
command: fetch
|
command: fetch
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -74,7 +79,8 @@ services:
|
|||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
|
networks:
|
||||||
|
- queues-development
|
||||||
command: notify
|
command: notify
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -93,7 +99,8 @@ services:
|
|||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
|
networks:
|
||||||
|
- queues-development
|
||||||
command: api
|
command: api
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -104,5 +111,9 @@ services:
|
|||||||
order: start-first
|
order: start-first
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
common-infra-nginx:
|
common-infra-nginx-development:
|
||||||
|
external: true
|
||||||
|
queues-development:
|
||||||
|
external: true
|
||||||
|
configurator:
|
||||||
external: true
|
external: true
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
|
networks:
|
||||||
|
- queues
|
||||||
command: poll
|
command: poll
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -25,7 +26,9 @@ services:
|
|||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
|
networks:
|
||||||
|
- queues
|
||||||
|
- configurator
|
||||||
command: worker
|
command: worker
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -40,7 +43,8 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
|
networks:
|
||||||
|
- queues
|
||||||
command: mailbox
|
command: mailbox
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -58,7 +62,8 @@ services:
|
|||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||||
DEBUG: "false"
|
DEBUG: "false"
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
|
networks:
|
||||||
|
- queues
|
||||||
command: fetch
|
command: fetch
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -80,7 +85,8 @@ services:
|
|||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||||
DEBUG: "false"
|
DEBUG: "false"
|
||||||
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
|
networks:
|
||||||
|
- queues
|
||||||
command: notify
|
command: notify
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
@@ -120,3 +126,7 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
common-infra-nginx:
|
common-infra-nginx:
|
||||||
external: true
|
external: true
|
||||||
|
queues:
|
||||||
|
external: true
|
||||||
|
configurator:
|
||||||
|
external: true
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ jobs:
|
|||||||
run: docker push mathwave/sprint-repo:ruz-bot
|
run: docker push mathwave/sprint-repo:ruz-bot
|
||||||
deploy-dev:
|
deploy-dev:
|
||||||
name: Deploy dev
|
name: Deploy dev
|
||||||
runs-on: [dev]
|
runs-on: [prod]
|
||||||
needs: push
|
needs: push
|
||||||
steps:
|
steps:
|
||||||
- name: login
|
- name: login
|
||||||
@@ -42,5 +42,4 @@ jobs:
|
|||||||
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 }}
|
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
|
||||||
QUEUES_TOKEN_DEV: ${{ secrets.QUEUES_TOKEN_DEV }}
|
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
|
|
||||||
|
|||||||
@@ -42,5 +42,4 @@ jobs:
|
|||||||
TELEGRAM_TOKEN_PROD: ${{ secrets.TELEGRAM_TOKEN_PROD }}
|
TELEGRAM_TOKEN_PROD: ${{ secrets.TELEGRAM_TOKEN_PROD }}
|
||||||
MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }}
|
MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }}
|
||||||
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
|
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
|
||||||
QUEUES_TOKEN_PROD: ${{ secrets.QUEUES_TOKEN_PROD }}
|
|
||||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml ruz-bot
|
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml ruz-bot
|
||||||
|
|||||||
@@ -5,14 +5,14 @@ from daemons import base
|
|||||||
from utils import queues
|
from utils import queues
|
||||||
|
|
||||||
|
|
||||||
class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
|
class Daemon(base.Daemon, queues.TasksHandlerMixin):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
self.bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def queue_name(self):
|
def queue_name(self):
|
||||||
return 'pizda_bot_mailbox'
|
return 'ruz_bot_mailbox'
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
self.poll()
|
self.poll()
|
||||||
@@ -25,4 +25,7 @@ class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
|
|||||||
reply_markup = payload.get('reply_markup')
|
reply_markup = payload.get('reply_markup')
|
||||||
if reply_markup:
|
if reply_markup:
|
||||||
body['reply_markup'] = reply_markup
|
body['reply_markup'] = reply_markup
|
||||||
self.bot.send_message(**body, parse_mode='Markdown')
|
try:
|
||||||
|
self.bot.send_message(**body, parse_mode='Markdown')
|
||||||
|
except Exception as exc:
|
||||||
|
print('Error', str(exc))
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from telebot import types
|
|||||||
from utils import queues
|
from utils import queues
|
||||||
|
|
||||||
|
|
||||||
class Daemon(base.BaseDaemon):
|
class Daemon(base.Daemon):
|
||||||
def execute(self):
|
def execute(self):
|
||||||
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
||||||
@bot.message_handler()
|
@bot.message_handler()
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import json
|
|||||||
from telebot.types import Message
|
from telebot.types import Message
|
||||||
|
|
||||||
|
|
||||||
class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
|
class Daemon(base.Daemon, queues.TasksHandlerMixin):
|
||||||
@property
|
@property
|
||||||
def queue_name(self):
|
def queue_name(self):
|
||||||
return 'ruz_bot_worker'
|
return 'ruz_bot_worker'
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from daemons.bot import bot
|
|
||||||
from helpers import now
|
from helpers import now
|
||||||
from helpers.mongo import mongo
|
from helpers.mongo import mongo
|
||||||
|
from utils import queues
|
||||||
|
|
||||||
|
|
||||||
def try_parse(message: str) -> Optional[int]:
|
def try_parse(message: str) -> Optional[int]:
|
||||||
@@ -84,7 +84,7 @@ class Processor:
|
|||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
mongo.users_collection.update_one({"yandex_code": code}, {"$set": {"yandex_id": self.user_id, "yandex_code": None}})
|
mongo.users_collection.update_one({"yandex_code": code}, {"$set": {"yandex_id": self.user_id, "yandex_code": None}})
|
||||||
bot.send_message(user['chat_id'], "Алиса успешно подключена!")
|
queues.set_task('ruz_bot_mailbox', {'text': "Алиса успешно подключена!", 'chat_id': user["chat_id"]}, 1)
|
||||||
lesson = self.get_lesson_for_user(user['chat_id'])
|
lesson = self.get_lesson_for_user(user['chat_id'])
|
||||||
if lesson is None:
|
if lesson is None:
|
||||||
return {
|
return {
|
||||||
@@ -94,4 +94,4 @@ class Processor:
|
|||||||
return {
|
return {
|
||||||
"text": f'Отлично, теперь я могу подсказывать тебе расписание. Твое ближайшее занятие в {lesson["begin"].strftime("%A %d %B %H:%M")}: {lesson["discipline"].replace("(рус)", "").replace("(анг)", "")}',
|
"text": f'Отлично, теперь я могу подсказывать тебе расписание. Твое ближайшее занятие в {lesson["begin"].strftime("%A %d %B %H:%M")}: {lesson["discipline"].replace("(рус)", "").replace("(анг)", "")}',
|
||||||
"end_session": True
|
"end_session": True
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,11 +15,8 @@ class PlatformClient:
|
|||||||
self.stage = stage
|
self.stage = stage
|
||||||
self.configs = configs
|
self.configs = configs
|
||||||
self.experiments = experiments
|
self.experiments = experiments
|
||||||
self.endpoint = 'https://platform.sprinthub.ru/'
|
self.endpoint = 'http://configurator/'
|
||||||
self.configs_url = urllib.parse.urljoin(self.endpoint, 'configs/get')
|
self.fetch_url = urllib.parse.urljoin(self.endpoint, '/api/v1/fetch')
|
||||||
self.experiments_url = urllib.parse.urljoin(self.endpoint, 'experiments/get')
|
|
||||||
self.staff_url = urllib.parse.urljoin(self.endpoint, 'is_staff')
|
|
||||||
self.fetch_url = urllib.parse.urljoin(self.endpoint, 'fetch')
|
|
||||||
self.config_storage = {}
|
self.config_storage = {}
|
||||||
self.experiment_storage = {}
|
self.experiment_storage = {}
|
||||||
self.staff_storage = {}
|
self.staff_storage = {}
|
||||||
@@ -44,7 +41,6 @@ class PlatformClient:
|
|||||||
try:
|
try:
|
||||||
response = get(
|
response = get(
|
||||||
url,
|
url,
|
||||||
headers={'X-Security-Token': self.platform_security_token},
|
|
||||||
params=params
|
params=params
|
||||||
)
|
)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
|
|||||||
@@ -1,18 +1,13 @@
|
|||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
stage = os.getenv("STAGE", 'local')
|
stage = os.getenv("STAGE", 'local')
|
||||||
if stage == 'development':
|
if stage == 'local':
|
||||||
QUEUES_URL = 'https://queues.develop.sprinthub.ru'
|
QUEUES_URL = 'http://localhost:1239'
|
||||||
elif stage == 'production':
|
|
||||||
QUEUES_URL = 'https://queues.sprinthub.ru'
|
|
||||||
else:
|
else:
|
||||||
QUEUES_URL = None
|
QUEUES_URL = 'http://queues:1239'
|
||||||
|
|
||||||
token = os.getenv('QUEUES_TOKEN')
|
|
||||||
|
|
||||||
|
|
||||||
class QueuesException(Exception):
|
class QueuesException(Exception):
|
||||||
@@ -22,29 +17,22 @@ class QueuesException(Exception):
|
|||||||
class TasksHandlerMixin:
|
class TasksHandlerMixin:
|
||||||
def poll(self):
|
def poll(self):
|
||||||
while True:
|
while True:
|
||||||
if QUEUES_URL is None:
|
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name}).json()
|
||||||
data = {'payload': json.loads(input('Input message: '))}
|
task = response.get('task')
|
||||||
else:
|
if not task:
|
||||||
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name, 'X-Queues-Token': token})
|
time.sleep(0.2)
|
||||||
if response.status_code == 404:
|
continue
|
||||||
time.sleep(0.2)
|
|
||||||
continue
|
|
||||||
if response.status_code == 403:
|
|
||||||
raise NotImplemented('QUEUE_TOKEN is incorrect')
|
|
||||||
data = response.json()
|
|
||||||
try:
|
try:
|
||||||
self.process(data['payload'])
|
self.process(task['payload'])
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
print(f'Error processing message id={data["id"]}, payload={data["payload"]}, exc={exc}')
|
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
|
||||||
continue
|
|
||||||
if QUEUES_URL is None:
|
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': data['id']}, headers={'X-Queues-Token': token})
|
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
|
||||||
except:
|
except:
|
||||||
print(f'Failed to finish task id={data["id"]}')
|
print(f'Failed to finish task id={task["id"]}')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def queue_name(self):
|
def queue_name(self):
|
||||||
@@ -55,7 +43,7 @@ class TasksHandlerMixin:
|
|||||||
|
|
||||||
|
|
||||||
def set_task(queue_name: str, payload: dict, seconds_to_execute: int, delay: int|None = None):
|
def set_task(queue_name: str, payload: dict, seconds_to_execute: int, delay: int|None = None):
|
||||||
resp = requests.post(f'{QUEUES_URL}/api/v1/put', headers={'queue': queue_name, 'X-Queues-Token': token}, json={
|
resp = requests.post(f'{QUEUES_URL}/api/v1/put', headers={'queue': queue_name}, json={
|
||||||
'payload': payload,
|
'payload': payload,
|
||||||
'seconds_to_execute': seconds_to_execute,
|
'seconds_to_execute': seconds_to_execute,
|
||||||
'delay': delay,
|
'delay': delay,
|
||||||
|
|||||||
Reference in New Issue
Block a user