Compare commits

..

No commits in common. "7238d725dd689088e9af386d84292b2b955b6726" and "4a3fa3c2ab2ae683464cc7a515d712dabd058ee4" have entirely different histories.

5 changed files with 34 additions and 35 deletions

View File

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

View File

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

View File

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

View File

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