fx
All checks were successful
Deploy Dev / Build (pull_request) Successful in 6s
Deploy Dev / Push (pull_request) Successful in 9s
Deploy Dev / Deploy dev (pull_request) Successful in 17s

This commit is contained in:
Egor Matveev 2025-06-15 02:42:23 +03:00
parent 151327ae0a
commit c20c5546fe
2 changed files with 22 additions and 38 deletions

View File

@ -1,27 +0,0 @@
from queue import Queue
from threading import Thread
class MetricsSender(Thread):
queue: Queue = Queue()
def put(self, metric):
self.queue.put(metric)
def run(self):
while True:
metric = self.queue.get()
if not metric:
continue
try:
metric = metric()
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}')
metrics_sender = MetricsSender()
metrics_sender.start()

View File

@ -1,8 +1,8 @@
import datetime
import os
from threading import Thread
import requests
import time
from utils.metrics import metrics_sender
stage = os.getenv("STAGE", 'local')
@ -17,6 +17,26 @@ class QueuesException(Exception):
class TasksHandlerMixin:
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,
'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}')
def send_metric(self, start, success, end):
Thread(target=self._send_metric, args=(start, success, end)).start()
def poll(self):
while True:
try:
@ -43,16 +63,7 @@ class TasksHandlerMixin:
raise QueuesException
except:
print(f'Failed to finish task id={task["id"]}')
metrics_sender.put(lambda: 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,
}))
self.send_metric(start, success, end)
@property
def queue_name(self):