fix
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
import zoneinfo
|
||||
import requests
|
||||
import time
|
||||
@@ -18,13 +19,9 @@ class QueuesException(Exception):
|
||||
|
||||
|
||||
class TasksHandlerMixin:
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.executor = ThreadPoolExecutor(max_workers=4)
|
||||
|
||||
def _send_metric(self, start: datetime.datetime, end: datetime.datetime, success: bool):
|
||||
try:
|
||||
resp = requests.post('http://monitoring:1237/api/v1/metrics/task', json={
|
||||
with open(f'/usr/src/{uuid.uuid4()}.json', 'w') as fp:
|
||||
fp.write(json.dumps({
|
||||
'service': 'botalka',
|
||||
'queue': self.queue_name,
|
||||
'success': success,
|
||||
@@ -32,13 +29,7 @@ class TasksHandlerMixin:
|
||||
"success": success,
|
||||
"execution_time_ms": (end - start).microseconds // 1000,
|
||||
"environment": stage,
|
||||
})
|
||||
if resp.status_code == 202:
|
||||
print("Metric ok")
|
||||
else:
|
||||
print(f'metric not ok: {resp.status_code}')
|
||||
except Exception as e:
|
||||
print(f"Error sending metric: {e}")
|
||||
}))
|
||||
|
||||
def poll(self):
|
||||
while True:
|
||||
@@ -69,7 +60,7 @@ class TasksHandlerMixin:
|
||||
print(f'finish task with id {task["id"]}')
|
||||
except:
|
||||
print(f'Failed to finish task id={task["id"]}')
|
||||
self.executor.submit(self._send_metric, start, end, success)
|
||||
self._send_metric(start, end, success)
|
||||
|
||||
@property
|
||||
def queue_name(self):
|
||||
|
||||
Reference in New Issue
Block a user