Compare commits
42 Commits
dev
...
77fe38acc0
| Author | SHA1 | Date | |
|---|---|---|---|
| 77fe38acc0 | |||
|
|
d91ae82f6e | ||
| 64db408144 | |||
|
|
46031265e0 | ||
| bd1e26cb50 | |||
| 5102564b84 | |||
| 733ab721d6 | |||
| 57c012cb44 | |||
| 96eef035c5 | |||
| cbbd1462cd | |||
| 5c6142202b | |||
| 723a230ced | |||
| c32bfad431 | |||
| 0f58ed101e | |||
| aebaa7e546 | |||
| 80462d2a61 | |||
| f0399196b5 | |||
| 7b806075c7 | |||
| 2eb13d9443 | |||
| 49634845a1 | |||
| e02646a1a2 | |||
| 1b7e8686e4 | |||
| cf12a0dca8 | |||
| 0a97e56539 | |||
| a5d3bd08c1 | |||
| ad74b8de7a | |||
| 05d9cdc7b1 | |||
| a209246513 | |||
| 0922b5a4a4 | |||
| 2ee74e70ac | |||
| da93092232 | |||
| 60b933496f | |||
| 82b99ae803 | |||
| c8f65a0ebb | |||
| 6401a40f11 | |||
| 32197fd699 | |||
| e69ee8767a | |||
| 54f7581657 | |||
| c6a2710087 | |||
| 42fc5552ab | |||
| 499eed49e0 | |||
| 349df7eb17 |
@@ -22,6 +22,7 @@ services:
|
||||
networks:
|
||||
- configurator
|
||||
- queues-development
|
||||
- monitoring
|
||||
environment:
|
||||
STAGE: "development"
|
||||
command: mailbox
|
||||
@@ -38,3 +39,5 @@ networks:
|
||||
external: true
|
||||
queues-development:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
import zoneinfo
|
||||
import requests
|
||||
import time
|
||||
|
||||
@@ -20,24 +16,6 @@ class QueuesException(Exception):
|
||||
|
||||
|
||||
class TasksHandlerMixin:
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.executor = ThreadPoolExecutor(max_workers=1)
|
||||
|
||||
def _send_metric(self, start: datetime.datetime, end: datetime.datetime, success: bool):
|
||||
def send():
|
||||
requests.post(f'{QUEUES_URL}/api/v1/metric', json={
|
||||
'service': 'botalka',
|
||||
'queue': self.queue_name,
|
||||
'success': success,
|
||||
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
||||
"success": success,
|
||||
"execution_time_ms": (end - start).microseconds // 1000,
|
||||
"environment": stage,
|
||||
})
|
||||
|
||||
self.executor.submit(send)
|
||||
|
||||
def poll(self):
|
||||
while True:
|
||||
try:
|
||||
@@ -50,24 +28,36 @@ class TasksHandlerMixin:
|
||||
if not task:
|
||||
time.sleep(0.2)
|
||||
continue
|
||||
start = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
start = datetime.datetime.now()
|
||||
try:
|
||||
print(f'process task with id {task["id"]}, attempt {task["attempt"]}')
|
||||
self.process(task['payload'])
|
||||
success = True
|
||||
except Exception as exc:
|
||||
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
|
||||
success = False
|
||||
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
if success:
|
||||
try:
|
||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||
if resp.status_code != 202:
|
||||
raise QueuesException
|
||||
print(f'finish task with id {task["id"]}')
|
||||
except:
|
||||
print(f'Failed to finish task id={task["id"]}')
|
||||
self._send_metric(start, end, success)
|
||||
end = datetime.datetime.now()
|
||||
try:
|
||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||
if resp.status_code != 202:
|
||||
raise QueuesException
|
||||
except:
|
||||
print(f'Failed to finish task id={task["id"]}')
|
||||
try:
|
||||
metric = requests.post('http://monitoring:1237/api/v1/metrics/task', json={
|
||||
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
||||
'service': 'botalka',
|
||||
'environment': stage,
|
||||
'queue': self.queue_name,
|
||||
'success': success,
|
||||
'execution_time_ms': (end - start).microseconds // 1000,
|
||||
})
|
||||
if metric.status_code == 202:
|
||||
print('metric ok')
|
||||
else:
|
||||
print(f'metric failed: {metric.status_code}')
|
||||
except Exception as e:
|
||||
print(f'metric failed: {e}')
|
||||
|
||||
|
||||
@property
|
||||
def queue_name(self):
|
||||
|
||||
Reference in New Issue
Block a user