Compare commits

..

5 Commits

Author SHA1 Message Date
4a3fa3c2ab Merge pull request 'fix' (#8) from queues into dev
Reviewed-on: #8
2024-11-17 23:08:08 +03:00
45e1c0fe8a Merge pull request 'fix' (#7) from queues into dev
Reviewed-on: #7
2024-11-17 23:03:48 +03:00
989f7026ad Merge pull request 'fix' (#6) from queues into dev
Reviewed-on: #6
2024-11-17 22:58:40 +03:00
90378be7c5 Merge pull request 'queues' (#5) from queues into dev
Reviewed-on: #5
2024-11-17 22:56:27 +03:00
95e4f5e8da Merge pull request 'deploy' (#2) from master into dev
Reviewed-on: #2
2024-10-11 07:05:29 +03:00
7 changed files with 48 additions and 54 deletions

View File

@@ -8,8 +8,7 @@ services:
environment:
STAGE: "development"
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
networks:
- queues-development
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
command: poll
deploy:
mode: replicated
@@ -26,9 +25,7 @@ services:
STAGE: "development"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
networks:
- queues-development
- configurator
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
command: worker
deploy:
mode: replicated
@@ -43,8 +40,7 @@ services:
environment:
STAGE: "development"
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
networks:
- queues-development
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
command: mailbox
deploy:
mode: replicated
@@ -61,8 +57,7 @@ services:
STAGE: "development"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
networks:
- queues-development
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
command: fetch
deploy:
mode: replicated
@@ -79,8 +74,7 @@ services:
STAGE: "development"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
networks:
- queues-development
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
command: notify
deploy:
mode: replicated
@@ -99,8 +93,7 @@ services:
STAGE: "development"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
networks:
- queues-development
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
command: api
deploy:
mode: replicated
@@ -111,9 +104,5 @@ services:
order: start-first
networks:
common-infra-nginx-development:
external: true
queues-development:
external: true
configurator:
common-infra-nginx:
external: true

View File

@@ -8,8 +8,7 @@ services:
environment:
STAGE: "production"
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
networks:
- queues
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
command: poll
deploy:
mode: replicated
@@ -26,9 +25,7 @@ services:
STAGE: "production"
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
networks:
- queues
- configurator
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
command: worker
deploy:
mode: replicated
@@ -43,8 +40,7 @@ services:
environment:
STAGE: "production"
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
networks:
- queues
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
command: mailbox
deploy:
mode: replicated
@@ -62,8 +58,7 @@ services:
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
DEBUG: "false"
networks:
- queues
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
command: fetch
deploy:
mode: replicated
@@ -85,8 +80,7 @@ services:
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
DEBUG: "false"
networks:
- queues
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
command: notify
deploy:
mode: replicated
@@ -126,7 +120,3 @@ services:
networks:
common-infra-nginx:
external: true
queues:
external: true
configurator:
external: true

View File

@@ -28,7 +28,7 @@ jobs:
run: docker push mathwave/sprint-repo:ruz-bot
deploy-dev:
name: Deploy dev
runs-on: [prod]
runs-on: [dev]
needs: push
steps:
- name: login
@@ -42,4 +42,5 @@ jobs:
TELEGRAM_TOKEN_DEV: ${{ secrets.TELEGRAM_TOKEN_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
QUEUES_TOKEN_DEV: ${{ secrets.QUEUES_TOKEN_DEV }}
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml ruz-bot

View File

@@ -42,4 +42,5 @@ jobs:
TELEGRAM_TOKEN_PROD: ${{ secrets.TELEGRAM_TOKEN_PROD }}
MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }}
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

View File

@@ -25,7 +25,4 @@ class Daemon(base.Daemon, queues.TasksHandlerMixin):
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))

View File

@@ -15,8 +15,11 @@ class PlatformClient:
self.stage = stage
self.configs = configs
self.experiments = experiments
self.endpoint = 'http://configurator/'
self.fetch_url = urllib.parse.urljoin(self.endpoint, '/api/v1/fetch')
self.endpoint = 'https://platform.sprinthub.ru/'
self.configs_url = urllib.parse.urljoin(self.endpoint, 'configs/get')
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.experiment_storage = {}
self.staff_storage = {}
@@ -41,6 +44,7 @@ class PlatformClient:
try:
response = get(
url,
headers={'X-Security-Token': self.platform_security_token},
params=params
)
if response.status_code == 200:

View File

@@ -1,13 +1,18 @@
import json
import os
import requests
import time
stage = os.getenv("STAGE", 'local')
if stage == 'local':
QUEUES_URL = 'http://localhost:1239'
if stage == 'development':
QUEUES_URL = 'https://queues.develop.sprinthub.ru'
elif stage == 'production':
QUEUES_URL = 'https://queues.sprinthub.ru'
else:
QUEUES_URL = 'http://queues:1239'
QUEUES_URL = None
token = os.getenv('QUEUES_TOKEN')
class QueuesException(Exception):
@@ -17,22 +22,29 @@ class QueuesException(Exception):
class TasksHandlerMixin:
def poll(self):
while True:
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name}).json()
task = response.get('task')
if not task:
if QUEUES_URL is None:
data = {'payload': json.loads(input('Input message: '))}
else:
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name, 'X-Queues-Token': token})
if response.status_code == 404:
time.sleep(0.2)
continue
if response.status_code == 403:
raise NotImplemented('QUEUE_TOKEN is incorrect')
data = response.json()
try:
self.process(task['payload'])
self.process(data['payload'])
except Exception as exc:
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
print(f'Error processing message id={data["id"]}, payload={data["payload"]}, exc={exc}')
continue
if QUEUES_URL is None:
continue
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': data['id']}, headers={'X-Queues-Token': token})
if resp.status_code != 202:
raise QueuesException
except:
print(f'Failed to finish task id={task["id"]}')
print(f'Failed to finish task id={data["id"]}')
@property
def queue_name(self):
@@ -43,7 +55,7 @@ class TasksHandlerMixin:
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}, json={
resp = requests.post(f'{QUEUES_URL}/api/v1/put', headers={'queue': queue_name, 'X-Queues-Token': token}, json={
'payload': payload,
'seconds_to_execute': seconds_to_execute,
'delay': delay,