Compare commits
20 Commits
metric
...
335e68beb7
| Author | SHA1 | Date | |
|---|---|---|---|
| 335e68beb7 | |||
|
|
357ab5576f | ||
| 860e06226a | |||
|
|
d45e8c8a35 | ||
| 944ac1301b | |||
|
|
73c8466a50 | ||
|
|
c20c5546fe | ||
|
|
151327ae0a | ||
|
|
d91ae82f6e | ||
|
|
46031265e0 | ||
| 2a7ad6345f | |||
| 39bca8ead9 | |||
| 43184acb16 | |||
| fb762e62e9 | |||
| 56eb2f60ee | |||
| cf1f92dcc9 | |||
| 48489e607e | |||
| 185ce0c5ce | |||
| cab8e15ba8 | |||
| d3d92f56ee |
@@ -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
|
||||
|
||||
@@ -22,6 +22,7 @@ services:
|
||||
networks:
|
||||
- configurator
|
||||
- queues
|
||||
- monitoring
|
||||
environment:
|
||||
STAGE: "production"
|
||||
command: mailbox
|
||||
@@ -38,3 +39,5 @@ networks:
|
||||
external: true
|
||||
queues:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import traceback
|
||||
import uuid
|
||||
from threading import Thread
|
||||
import zoneinfo
|
||||
import requests
|
||||
import time
|
||||
@@ -21,23 +18,27 @@ 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={
|
||||
def _send_metric(self, start, success, end):
|
||||
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,
|
||||
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
||||
"success": success,
|
||||
"execution_time_ms": (end - start).microseconds // 1000,
|
||||
"environment": stage,
|
||||
'execution_time_ms': (end - start).microseconds // 1000,
|
||||
})
|
||||
|
||||
self.executor.submit(send)
|
||||
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}')
|
||||
|
||||
def send_metric(self, start, success, end):
|
||||
# Thread(target=self._send_metric, args=(start, success, end)).start()
|
||||
# self._send_metric(start, success, end)
|
||||
...
|
||||
|
||||
def poll(self):
|
||||
while True:
|
||||
@@ -51,25 +52,21 @@ class TasksHandlerMixin:
|
||||
if not task:
|
||||
time.sleep(0.2)
|
||||
continue
|
||||
start = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
start = datetime.datetime.now().astimezone(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
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}')
|
||||
traceback.print_stack()
|
||||
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().astimezone(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
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"]}')
|
||||
self.send_metric(start, success, end)
|
||||
|
||||
@property
|
||||
def queue_name(self):
|
||||
|
||||
Reference in New Issue
Block a user