metric #82

Merged
emmatveev merged 17 commits from metric into master 2025-07-17 23:32:44 +03:00
5 changed files with 11 additions and 44 deletions
Showing only changes of commit 33901b0a82 - Show all commits

View File

@ -5,6 +5,4 @@ COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
COPY . . COPY . .
ENV PYTHONUNBUFFERED 1 ENV PYTHONUNBUFFERED 1
RUN mkdir /usr/src/metrics ENTRYPOINT ["python", "main.py"]
RUN chmod 777 run.sh
ENTRYPOINT ["./run.sh"]

View File

@ -1,22 +0,0 @@
import os
from requests import post
import json
from daemons import base
class Daemon(base.Daemon):
def execute(self):
while True:
for file in os.listdir('/usr/src/metrics'):
data = open(f'/usr/src/metrics/{file}', 'r').read()
payload = json.loads(data)
resp = post('http://queues:1239/api/v1/metric', json=payload)
if resp.status_code == 202:
print("Metric ok")
else:
print(f'metric not ok: {resp.status_code}')
os.remove(f'/usr/src/metrics/{file}')
break

View File

@ -11,9 +11,6 @@ if __name__ == '__main__':
elif arg == 'mailbox': elif arg == 'mailbox':
print("mailbox is starting") print("mailbox is starting")
from daemons.mailbox import Daemon from daemons.mailbox import Daemon
elif arg == 'metrics':
print("metrics is starting")
from daemons.metrics import Daemon
else: else:
raise ValueError(f"Unknown param {arg}") raise ValueError(f"Unknown param {arg}")

13
run.sh
View File

@ -1,13 +0,0 @@
#!/bin/bash
# Start the first process
python3 main.py $1 &
# Start the second process
python3 main.py metrics &
# Wait for any process to exit
wait -n
# Exit with status of process that exited first
exit $?

View File

@ -1,3 +1,4 @@
from concurrent.futures import ThreadPoolExecutor
import datetime import datetime
import json import json
import os import os
@ -19,9 +20,13 @@ class QueuesException(Exception):
class TasksHandlerMixin: 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_metric(self, start: datetime.datetime, end: datetime.datetime, success: bool):
with open(f'/usr/src/{uuid.uuid4()}.json', 'w') as fp: def send():
fp.write(json.dumps({ requests.post(f'{QUEUES_URL}/api/v1/metric', json={
'service': 'botalka', 'service': 'botalka',
'queue': self.queue_name, 'queue': self.queue_name,
'success': success, 'success': success,
@ -29,7 +34,9 @@ class TasksHandlerMixin:
"success": success, "success": success,
"execution_time_ms": (end - start).microseconds // 1000, "execution_time_ms": (end - start).microseconds // 1000,
"environment": stage, "environment": stage,
})) })
self.executor.submit(send)
def poll(self): def poll(self):
while True: while True: