Compare commits
3 Commits
master
...
57669cf6bd
| Author | SHA1 | Date | |
|---|---|---|---|
| 57669cf6bd | |||
| 9a55a4f822 | |||
| 8bd6dc2701 |
@@ -22,6 +22,7 @@ services:
|
||||
networks:
|
||||
- configurator
|
||||
- queues-development
|
||||
- monitoring
|
||||
environment:
|
||||
STAGE: "development"
|
||||
command: mailbox
|
||||
@@ -38,3 +39,5 @@ networks:
|
||||
external: true
|
||||
queues-development:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
||||
@@ -22,6 +22,7 @@ services:
|
||||
networks:
|
||||
- configurator
|
||||
- queues
|
||||
- monitoring
|
||||
environment:
|
||||
STAGE: "production"
|
||||
command: mailbox
|
||||
@@ -38,3 +39,5 @@ networks:
|
||||
external: true
|
||||
queues:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
||||
@@ -5,4 +5,6 @@ COPY requirements.txt requirements.txt
|
||||
RUN pip install -r requirements.txt
|
||||
COPY . .
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
ENTRYPOINT ["python", "main.py"]
|
||||
RUN mkdir /usr/src/metrics
|
||||
RUN chmod 777 run.sh
|
||||
ENTRYPOINT ["./run.sh"]
|
||||
|
||||
22
daemons/metrics.py
Normal file
22
daemons/metrics.py
Normal file
@@ -0,0 +1,22 @@
|
||||
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,6 +11,9 @@ if __name__ == '__main__':
|
||||
elif arg == 'mailbox':
|
||||
print("mailbox is starting")
|
||||
from daemons.mailbox import Daemon
|
||||
elif arg == 'metrics':
|
||||
print("metrics is starting")
|
||||
from daemons.metrics import Daemon
|
||||
else:
|
||||
raise ValueError(f"Unknown param {arg}")
|
||||
|
||||
|
||||
13
run.sh
Normal file
13
run.sh
Normal file
@@ -0,0 +1,13 @@
|
||||
#!/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,8 +1,6 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import traceback
|
||||
import uuid
|
||||
import zoneinfo
|
||||
import requests
|
||||
@@ -21,13 +19,9 @@ class QueuesException(Exception):
|
||||
|
||||
|
||||
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():
|
||||
requests.post(f'{QUEUES_URL}/api/v1/metric', json={
|
||||
with open(f'/usr/src/{uuid.uuid4()}.json', 'w') as fp:
|
||||
fp.write(json.dumps({
|
||||
'service': 'botalka',
|
||||
'queue': self.queue_name,
|
||||
'success': success,
|
||||
@@ -35,9 +29,7 @@ class TasksHandlerMixin:
|
||||
"success": success,
|
||||
"execution_time_ms": (end - start).microseconds // 1000,
|
||||
"environment": stage,
|
||||
})
|
||||
|
||||
self.executor.submit(send)
|
||||
}))
|
||||
|
||||
def poll(self):
|
||||
while True:
|
||||
@@ -58,7 +50,6 @@ class TasksHandlerMixin:
|
||||
success = True
|
||||
except Exception as exc:
|
||||
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
|
||||
traceback.print_stack()
|
||||
success = False
|
||||
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
if success:
|
||||
@@ -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._send_metric(start, end, success)
|
||||
# self._send_metric(start, end, success)
|
||||
|
||||
@property
|
||||
def queue_name(self):
|
||||
|
||||
Reference in New Issue
Block a user