Compare commits
20 Commits
prod
...
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:
|
networks:
|
||||||
- configurator
|
- configurator
|
||||||
- queues-development
|
- queues-development
|
||||||
|
- monitoring
|
||||||
environment:
|
environment:
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
command: mailbox
|
command: mailbox
|
||||||
@@ -38,3 +39,5 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
queues-development:
|
queues-development:
|
||||||
external: true
|
external: true
|
||||||
|
monitoring:
|
||||||
|
external: true
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- configurator
|
- configurator
|
||||||
- queues
|
- queues
|
||||||
|
- monitoring
|
||||||
environment:
|
environment:
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
command: mailbox
|
command: mailbox
|
||||||
@@ -38,3 +39,5 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
queues:
|
queues:
|
||||||
external: true
|
external: true
|
||||||
|
monitoring:
|
||||||
|
external: true
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
from concurrent.futures import ThreadPoolExecutor
|
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import traceback
|
from threading import Thread
|
||||||
import uuid
|
|
||||||
import zoneinfo
|
import zoneinfo
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
@@ -21,23 +18,27 @@ class QueuesException(Exception):
|
|||||||
|
|
||||||
|
|
||||||
class TasksHandlerMixin:
|
class TasksHandlerMixin:
|
||||||
def __init__(self, *args, **kwargs):
|
def _send_metric(self, start, success, end):
|
||||||
super().__init__(*args, **kwargs)
|
try:
|
||||||
self.executor = ThreadPoolExecutor(max_workers=1)
|
metric = requests.post('http://monitoring:1237/api/v1/metrics/task', json={
|
||||||
|
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
||||||
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',
|
'service': 'botalka',
|
||||||
|
'environment': stage,
|
||||||
'queue': self.queue_name,
|
'queue': self.queue_name,
|
||||||
'success': success,
|
'success': success,
|
||||||
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
'execution_time_ms': (end - start).microseconds // 1000,
|
||||||
"success": success,
|
|
||||||
"execution_time_ms": (end - start).microseconds // 1000,
|
|
||||||
"environment": stage,
|
|
||||||
})
|
})
|
||||||
|
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}')
|
||||||
|
|
||||||
self.executor.submit(send)
|
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):
|
def poll(self):
|
||||||
while True:
|
while True:
|
||||||
@@ -51,25 +52,21 @@ class TasksHandlerMixin:
|
|||||||
if not task:
|
if not task:
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
continue
|
continue
|
||||||
start = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
start = datetime.datetime.now().astimezone(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||||
try:
|
try:
|
||||||
print(f'process task with id {task["id"]}, attempt {task["attempt"]}')
|
|
||||||
self.process(task['payload'])
|
self.process(task['payload'])
|
||||||
success = True
|
success = True
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
|
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
|
||||||
traceback.print_stack()
|
|
||||||
success = False
|
success = False
|
||||||
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
end = datetime.datetime.now().astimezone(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||||
if success:
|
try:
|
||||||
try:
|
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
if resp.status_code != 202:
|
||||||
if resp.status_code != 202:
|
raise QueuesException
|
||||||
raise QueuesException
|
except:
|
||||||
print(f'finish task with id {task["id"]}')
|
print(f'Failed to finish task id={task["id"]}')
|
||||||
except:
|
self.send_metric(start, success, end)
|
||||||
print(f'Failed to finish task id={task["id"]}')
|
|
||||||
self._send_metric(start, end, success)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def queue_name(self):
|
def queue_name(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user