diff --git a/Dockerfile b/Dockerfile index cd17104..5375a3d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/daemons/metrics.py b/daemons/metrics.py deleted file mode 100644 index 304d0d6..0000000 --- a/daemons/metrics.py +++ /dev/null @@ -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 diff --git a/main.py b/main.py index b32aa51..b1f1648 100644 --- a/main.py +++ b/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}") diff --git a/run.sh b/run.sh deleted file mode 100644 index 1826c2e..0000000 --- a/run.sh +++ /dev/null @@ -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 $? \ No newline at end of file diff --git a/utils/queues.py b/utils/queues.py index 5adf3f4..fe64e36 100644 --- a/utils/queues.py +++ b/utils/queues.py @@ -1,3 +1,4 @@ +from concurrent.futures import ThreadPoolExecutor import datetime import json import os @@ -19,9 +20,13 @@ 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): - with open(f'/usr/src/{uuid.uuid4()}.json', 'w') as fp: - fp.write(json.dumps({ + def send(): + requests.post(f'{QUEUES_URL}/api/v1/metric', json={ 'service': 'botalka', 'queue': self.queue_name, 'success': success, @@ -29,7 +34,9 @@ class TasksHandlerMixin: "success": success, "execution_time_ms": (end - start).microseconds // 1000, "environment": stage, - })) + }) + + self.executor.submit(send) def poll(self): while True: