Compare commits

..

8 Commits

Author SHA1 Message Date
5042df4874 Merge pull request 'fix' (#12) from queues into dev
Reviewed-on: #12
2024-11-17 13:09:31 +03:00
7fd853f300 Merge pull request 'fix' (#11) from queues into dev
Reviewed-on: #11
2024-11-17 13:06:04 +03:00
6a0471dc15 Merge pull request 'add' (#10) from queues into dev
Reviewed-on: #10
2024-11-17 13:01:49 +03:00
cc290465a1 Merge pull request 'queues' (#9) from queues into dev
Reviewed-on: #9
2024-11-17 12:58:54 +03:00
1d0abfed03 Merge pull request 'fix' (#6) from master into dev
Reviewed-on: #6
2024-10-11 06:50:59 +03:00
e17c5ab7fe Merge pull request 'master' (#5) from master into dev
Reviewed-on: #5
2024-10-11 06:49:45 +03:00
ff7f0ffa91 Merge pull request 'fix' (#3) from master into dev
Reviewed-on: #3
2024-10-11 06:13:49 +03:00
71a12ef77b Merge pull request 'fix' (#2) from master into dev
All checks were successful
Build and push / Build (push) Successful in 5s
Build and push / Push (push) Successful in 8s
Reviewed-on: #2
2024-10-11 05:46:11 +03:00
7 changed files with 33 additions and 33 deletions

View File

@@ -9,8 +9,7 @@ services:
environment: environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
STAGE: "development" STAGE: "development"
networks: QUEUES_TOKEN: $QUEUES_TOKEN_DEV
- queues
deploy: deploy:
mode: replicated mode: replicated
restart_policy: restart_policy:
@@ -26,9 +25,8 @@ services:
MONGO_HOST: "mongo.develop.sprinthub.ru" MONGO_HOST: "mongo.develop.sprinthub.ru"
MONGO_PASSWORD: $MONGO_PASSWORD_DEV MONGO_PASSWORD: $MONGO_PASSWORD_DEV
STAGE: "development" STAGE: "development"
QUEUES_TOKEN: $QUEUES_TOKEN_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
networks:
- queues
deploy: deploy:
mode: replicated mode: replicated
restart_policy: restart_policy:
@@ -43,8 +41,7 @@ services:
environment: environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
STAGE: "development" STAGE: "development"
networks: QUEUES_TOKEN: $QUEUES_TOKEN_DEV
- queues
deploy: deploy:
mode: replicated mode: replicated
restart_policy: restart_policy:
@@ -75,5 +72,3 @@ services:
networks: networks:
common-infra-nginx: common-infra-nginx:
external: true external: true
queues:
external: true

View File

@@ -9,8 +9,7 @@ services:
environment: environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
STAGE: "production" STAGE: "production"
networks: QUEUES_TOKEN: $QUEUES_TOKEN_PROD
- queues
deploy: deploy:
mode: replicated mode: replicated
restart_policy: restart_policy:
@@ -26,9 +25,8 @@ services:
MONGO_HOST: "mongo.sprinthub.ru" MONGO_HOST: "mongo.sprinthub.ru"
MONGO_PASSWORD: $MONGO_PASSWORD_PROD MONGO_PASSWORD: $MONGO_PASSWORD_PROD
STAGE: "production" STAGE: "production"
QUEUES_TOKEN: $QUEUES_TOKEN_PROD
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
networks:
- queues
deploy: deploy:
mode: replicated mode: replicated
restart_policy: restart_policy:
@@ -43,8 +41,7 @@ services:
environment: environment:
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
STAGE: "production" STAGE: "production"
networks: QUEUES_TOKEN: $QUEUES_TOKEN_PROD
- queues
deploy: deploy:
mode: replicated mode: replicated
restart_policy: restart_policy:
@@ -75,5 +72,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 pizda-bot run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml pizda-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 pizda-bot run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml pizda-bot

View File

@@ -47,7 +47,7 @@ class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
def execute(self): def execute(self):
self.poll() self.poll()
def reply(self, text: str, chat_id: int, message_id: int): def reply(text: str, chat_id: int, message_id: int):
queues.set_task( queues.set_task(
'pizda_bot_mailbox', 'pizda_bot_mailbox',
{ {
@@ -59,7 +59,7 @@ class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
1, 1,
) )
def send(self, text: str, chat_id: int): def send(text: str, chat_id: int):
queues.set_task( queues.set_task(
'pizda_bot_mailbox', 'pizda_bot_mailbox',
{ {

View File

@@ -8,9 +8,6 @@ if arg == 'poll':
elif arg == 'worker': elif arg == 'worker':
from daemons import worker from daemons import worker
daemon = worker.Daemon() daemon = worker.Daemon()
elif arg == 'mailbox':
from daemons import mailbox
daemon = mailbox.Daemon()
else: else:
from api import app from api import app
app.run(host="0.0.0.0", port=1238) app.run(host="0.0.0.0", port=1238)

View File

@@ -5,10 +5,14 @@ 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):
@@ -18,18 +22,25 @@ 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']}) 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:
raise QueuesException raise QueuesException
except: except:
@@ -44,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,