Compare commits
9 Commits
8bd6dc2701
...
metric
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5581786225 | ||
|
|
66b4348957 | ||
|
|
33901b0a82 | ||
|
|
7b9d264ef9 | ||
|
|
a707a8b93e | ||
|
|
df3354a536 | ||
|
|
6a8f18300b | ||
|
|
b575ba5717 | ||
|
|
24b1d39e10 |
@@ -22,7 +22,6 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- configurator
|
- configurator
|
||||||
- queues-development
|
- queues-development
|
||||||
- monitoring
|
|
||||||
environment:
|
environment:
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
command: mailbox
|
command: mailbox
|
||||||
@@ -39,5 +38,3 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
queues-development:
|
queues-development:
|
||||||
external: true
|
external: true
|
||||||
monitoring:
|
|
||||||
external: true
|
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- configurator
|
- configurator
|
||||||
- queues
|
- queues
|
||||||
- monitoring
|
|
||||||
environment:
|
environment:
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
command: mailbox
|
command: mailbox
|
||||||
@@ -39,5 +38,3 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
queues:
|
queues:
|
||||||
external: true
|
external: true
|
||||||
monitoring:
|
|
||||||
external: true
|
|
||||||
|
|||||||
@@ -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"]
|
|
||||||
|
|||||||
@@ -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://monitoring:1237/api/v1/metrics/task', 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
|
|
||||||
3
main.py
3
main.py
@@ -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
13
run.sh
@@ -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 $?
|
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import traceback
|
||||||
import uuid
|
import uuid
|
||||||
import zoneinfo
|
import zoneinfo
|
||||||
import requests
|
import requests
|
||||||
@@ -19,9 +21,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 +35,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:
|
||||||
@@ -50,6 +58,7 @@ class TasksHandlerMixin:
|
|||||||
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(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||||
if success:
|
if success:
|
||||||
|
|||||||
Reference in New Issue
Block a user