Merge pull request 'fix' (#9) from queues into dev

Reviewed-on: #9
This commit is contained in:
emmatveev 2024-11-22 01:48:32 +03:00
commit 7238d725dd
5 changed files with 35 additions and 34 deletions

View File

@ -8,7 +8,8 @@ services:
environment:
STAGE: "development"
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
networks:
- queues
command: poll
deploy:
mode: replicated
@ -25,7 +26,8 @@ services:
STAGE: "development"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
networks:
- queues
command: worker
deploy:
mode: replicated
@ -40,7 +42,8 @@ services:
environment:
STAGE: "development"
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
networks:
- queues
command: mailbox
deploy:
mode: replicated
@ -57,7 +60,8 @@ services:
STAGE: "development"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
networks:
- queues
command: fetch
deploy:
mode: replicated
@ -74,7 +78,8 @@ services:
STAGE: "development"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
networks:
- queues
command: notify
deploy:
mode: replicated
@ -93,7 +98,8 @@ services:
STAGE: "development"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
networks:
- queues
command: api
deploy:
mode: replicated
@ -106,3 +112,5 @@ services:
networks:
common-infra-nginx:
external: true
queues:
external: true

View File

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

View File

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

View File

@ -42,5 +42,4 @@ 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

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