Compare commits
54 Commits
master
...
ec65d8ad8c
| Author | SHA1 | Date | |
|---|---|---|---|
| ec65d8ad8c | |||
|
|
88914893ce | ||
| 08bc931a5a | |||
|
|
5c1418ddd8 | ||
|
|
357ab5576f | ||
|
|
d45e8c8a35 | ||
| fbc9c0e0d7 | |||
|
|
73c8466a50 | ||
| 9b2e96d401 | |||
|
|
c20c5546fe | ||
| d27eed093b | |||
|
|
151327ae0a | ||
| 77fe38acc0 | |||
|
|
d91ae82f6e | ||
| 64db408144 | |||
|
|
46031265e0 | ||
| bd1e26cb50 | |||
| 5102564b84 | |||
| 733ab721d6 | |||
| 57c012cb44 | |||
| 96eef035c5 | |||
| cbbd1462cd | |||
| 5c6142202b | |||
| 723a230ced | |||
| c32bfad431 | |||
| 0f58ed101e | |||
| aebaa7e546 | |||
| 80462d2a61 | |||
| f0399196b5 | |||
| 7b806075c7 | |||
| 2eb13d9443 | |||
| 49634845a1 | |||
| e02646a1a2 | |||
| 1b7e8686e4 | |||
| cf12a0dca8 | |||
| 0a97e56539 | |||
| a5d3bd08c1 | |||
| ad74b8de7a | |||
| 05d9cdc7b1 | |||
| a209246513 | |||
| 0922b5a4a4 | |||
| 2ee74e70ac | |||
| da93092232 | |||
| 60b933496f | |||
| 82b99ae803 | |||
| c8f65a0ebb | |||
| 6401a40f11 | |||
| 32197fd699 | |||
| e69ee8767a | |||
| 54f7581657 | |||
| c6a2710087 | |||
| 42fc5552ab | |||
| 499eed49e0 | |||
| 349df7eb17 |
@@ -22,6 +22,7 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- configurator
|
- configurator
|
||||||
- queues-development
|
- queues-development
|
||||||
|
- monitoring
|
||||||
environment:
|
environment:
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
command: mailbox
|
command: mailbox
|
||||||
@@ -38,3 +39,5 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
queues-development:
|
queues-development:
|
||||||
external: true
|
external: true
|
||||||
|
monitoring:
|
||||||
|
external: true
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- configurator
|
- configurator
|
||||||
- queues
|
- queues
|
||||||
|
- monitoring
|
||||||
environment:
|
environment:
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
command: mailbox
|
command: mailbox
|
||||||
@@ -38,3 +39,5 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
queues:
|
queues:
|
||||||
external: true
|
external: true
|
||||||
|
monitoring:
|
||||||
|
external: true
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import traceback
|
|
||||||
import uuid
|
|
||||||
import zoneinfo
|
import zoneinfo
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
@@ -16,28 +13,34 @@ else:
|
|||||||
QUEUES_URL = 'http://queues:1239'
|
QUEUES_URL = 'http://queues:1239'
|
||||||
|
|
||||||
|
|
||||||
|
executor = ThreadPoolExecutor(max_workers=1)
|
||||||
|
|
||||||
|
|
||||||
class QueuesException(Exception):
|
class QueuesException(Exception):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
class TasksHandlerMixin:
|
class TasksHandlerMixin:
|
||||||
def __init__(self, *args, **kwargs):
|
def _send_metric(self, start, success, end):
|
||||||
super().__init__(*args, **kwargs)
|
try:
|
||||||
self.executor = ThreadPoolExecutor(max_workers=1)
|
metric = requests.post('http://monitoring:1237/api/v1/metrics/task', json={
|
||||||
|
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
||||||
def _send_metric(self, start: datetime.datetime, end: datetime.datetime, success: bool):
|
|
||||||
def send():
|
|
||||||
requests.post(f'{QUEUES_URL}/api/v1/metric', json={
|
|
||||||
'service': 'botalka',
|
'service': 'botalka',
|
||||||
|
'environment': stage,
|
||||||
'queue': self.queue_name,
|
'queue': self.queue_name,
|
||||||
'success': success,
|
'success': success,
|
||||||
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
'execution_time_ms': (end - start).microseconds // 1000,
|
||||||
"success": success,
|
|
||||||
"execution_time_ms": (end - start).microseconds // 1000,
|
|
||||||
"environment": stage,
|
|
||||||
})
|
})
|
||||||
|
if metric.status_code == 202:
|
||||||
|
print('metric ok')
|
||||||
|
else:
|
||||||
|
print(f'metric failed: {metric.status_code}')
|
||||||
|
except Exception as e:
|
||||||
|
print(f'metric failed: {e}')
|
||||||
|
|
||||||
self.executor.submit(send)
|
def send_metric(self, start, success, end):
|
||||||
|
# executor.submit(self._send_metric, start, success, end)
|
||||||
|
...
|
||||||
|
|
||||||
def poll(self):
|
def poll(self):
|
||||||
while True:
|
while True:
|
||||||
@@ -51,25 +54,21 @@ class TasksHandlerMixin:
|
|||||||
if not task:
|
if not task:
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
continue
|
continue
|
||||||
start = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
start = datetime.datetime.now().astimezone(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||||
try:
|
try:
|
||||||
print(f'process task with id {task["id"]}, attempt {task["attempt"]}')
|
|
||||||
self.process(task['payload'])
|
self.process(task['payload'])
|
||||||
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().astimezone(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||||
if success:
|
try:
|
||||||
try:
|
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
if resp.status_code != 202:
|
||||||
if resp.status_code != 202:
|
raise QueuesException
|
||||||
raise QueuesException
|
except:
|
||||||
print(f'finish task with id {task["id"]}')
|
print(f'Failed to finish task id={task["id"]}')
|
||||||
except:
|
self.send_metric(start, success, end)
|
||||||
print(f'Failed to finish task id={task["id"]}')
|
|
||||||
self._send_metric(start, end, success)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def queue_name(self):
|
def queue_name(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user