Compare commits
No commits in common. "8bd6dc270111db2fde83ee75255f734da1a3c710" and "ef1d60e3683674e93aa13f9d41798879d85c5ef7" have entirely different histories.
8bd6dc2701
...
ef1d60e368
@ -22,7 +22,6 @@ services:
|
||||
networks:
|
||||
- configurator
|
||||
- queues-development
|
||||
- monitoring
|
||||
environment:
|
||||
STAGE: "development"
|
||||
command: mailbox
|
||||
@ -39,5 +38,3 @@ networks:
|
||||
external: true
|
||||
queues-development:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
@ -22,7 +22,6 @@ services:
|
||||
networks:
|
||||
- configurator
|
||||
- queues
|
||||
- monitoring
|
||||
environment:
|
||||
STAGE: "production"
|
||||
command: mailbox
|
||||
@ -39,5 +38,3 @@ networks:
|
||||
external: true
|
||||
queues:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
@ -5,6 +5,4 @@ COPY requirements.txt requirements.txt
|
||||
RUN pip install -r requirements.txt
|
||||
COPY . .
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
RUN mkdir /usr/src/metrics
|
||||
RUN chmod 777 run.sh
|
||||
ENTRYPOINT ["./run.sh"]
|
||||
ENTRYPOINT ["python", "main.py"]
|
||||
|
@ -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':
|
||||
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
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,8 +1,4 @@
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import uuid
|
||||
import zoneinfo
|
||||
import requests
|
||||
import time
|
||||
|
||||
@ -19,18 +15,6 @@ class QueuesException(Exception):
|
||||
|
||||
|
||||
class TasksHandlerMixin:
|
||||
def _send_metric(self, start: datetime.datetime, end: datetime.datetime, success: bool):
|
||||
with open(f'/usr/src/{uuid.uuid4()}.json', 'w') as fp:
|
||||
fp.write(json.dumps({
|
||||
'service': 'botalka',
|
||||
'queue': self.queue_name,
|
||||
'success': success,
|
||||
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
||||
"success": success,
|
||||
"execution_time_ms": (end - start).microseconds // 1000,
|
||||
"environment": stage,
|
||||
}))
|
||||
|
||||
def poll(self):
|
||||
while True:
|
||||
try:
|
||||
@ -43,24 +27,17 @@ class TasksHandlerMixin:
|
||||
if not task:
|
||||
time.sleep(0.2)
|
||||
continue
|
||||
start = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
try:
|
||||
print(f'process task with id {task["id"]}, attempt {task["attempt"]}')
|
||||
self.process(task['payload'])
|
||||
success = True
|
||||
except Exception as exc:
|
||||
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
|
||||
success = False
|
||||
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
if success:
|
||||
try:
|
||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||
if resp.status_code != 202:
|
||||
raise QueuesException
|
||||
print(f'finish task with id {task["id"]}')
|
||||
except:
|
||||
print(f'Failed to finish task id={task["id"]}')
|
||||
self._send_metric(start, end, success)
|
||||
continue
|
||||
try:
|
||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||
if resp.status_code != 202:
|
||||
raise QueuesException
|
||||
except:
|
||||
print(f'Failed to finish task id={task["id"]}')
|
||||
|
||||
@property
|
||||
def queue_name(self):
|
||||
|
Loading…
Reference in New Issue
Block a user