mirror of
				https://github.com/catspace-dev/unicheckbot.git
				synced 2025-11-04 01:17:30 +03:00 
			
		
		
		
	report problems about api requests to admins, close #3
This commit is contained in:
		
							parent
							
								
									653cf725c7
								
							
						
					
					
						commit
						714dc2e4a2
					
				| 
						 | 
					@ -9,3 +9,7 @@ INFLUX_PORT = os.getenv("INFLUX_PORT", None)
 | 
				
			||||||
INFLUX_USERNAME = os.getenv("INFLUX_USERNAME", None)
 | 
					INFLUX_USERNAME = os.getenv("INFLUX_USERNAME", None)
 | 
				
			||||||
INFLUX_PASSWORD = os.getenv("INFLUX_PASSWORD", None)
 | 
					INFLUX_PASSWORD = os.getenv("INFLUX_PASSWORD", None)
 | 
				
			||||||
INFLUX_DB = os.getenv("INFLUX_DB", None)
 | 
					INFLUX_DB = os.getenv("INFLUX_DB", None)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Notifications
 | 
				
			||||||
 | 
					NOTIFICATION_BOT_TOKEN = os.getenv("NOTIFICATION_BOT_TOKEN")
 | 
				
			||||||
 | 
					NOTIFICATION_USERS = os.getenv("NOTIFICATION_USERS", "").split(",")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,10 +1,13 @@
 | 
				
			||||||
from httpx import AsyncClient, Timeout, Response, ConnectError
 | 
					from httpx import AsyncClient, Timeout, Response, ConnectError, ReadTimeout
 | 
				
			||||||
from typing import List
 | 
					from typing import List
 | 
				
			||||||
from core.coretypes import APINode
 | 
					from core.coretypes import APINode
 | 
				
			||||||
from ipaddress import ip_address
 | 
					from ipaddress import ip_address
 | 
				
			||||||
from contextlib import suppress
 | 
					from contextlib import suppress
 | 
				
			||||||
from loguru import logger
 | 
					from loguru import logger
 | 
				
			||||||
 | 
					from aiogram.bot import Bot
 | 
				
			||||||
from tgbot.handlers.metrics import push_api_request_status
 | 
					from tgbot.handlers.metrics import push_api_request_status
 | 
				
			||||||
 | 
					from tgbot.config import NOTIFICATION_BOT_TOKEN, NOTIFICATION_USERS
 | 
				
			||||||
 | 
					from traceback import format_exc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def check_int(value) -> bool:
 | 
					def check_int(value) -> bool:
 | 
				
			||||||
| 
						 | 
					@ -45,13 +48,20 @@ async def send_api_requests(endpoint: str, data: dict, nodes: List[APINode]):
 | 
				
			||||||
                result = await client.get(
 | 
					                result = await client.get(
 | 
				
			||||||
                    f"{node.address}/{endpoint}", params=data
 | 
					                    f"{node.address}/{endpoint}", params=data
 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
        except ConnectError as e:
 | 
					        except Exception as e:
 | 
				
			||||||
            logger.error(f"Node {node.address} got ConnectionError. Full exception: {e}")
 | 
					            logger.error(f"Node {node.address} got Error. Full exception: {e}")
 | 
				
			||||||
            # TODO: Report problems to admins
 | 
					            # We yield 500 response when get error
 | 
				
			||||||
            # We yield 500 response when backend is offline
 | 
					 | 
				
			||||||
            result = Response(500)
 | 
					            result = Response(500)
 | 
				
			||||||
 | 
					            await send_message_to_admins(f"Node {node.address} got error {e}. Full exception: ```{format_exc()}```")
 | 
				
			||||||
        await push_api_request_status(
 | 
					        await push_api_request_status(
 | 
				
			||||||
            result.status_code,
 | 
					            result.status_code,
 | 
				
			||||||
            endpoint
 | 
					            endpoint
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        yield result
 | 
					        yield result
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async def send_message_to_admins(message: str):
 | 
				
			||||||
 | 
					    bot = Bot(token=NOTIFICATION_BOT_TOKEN)
 | 
				
			||||||
 | 
					    for user in NOTIFICATION_USERS:
 | 
				
			||||||
 | 
					        logger.info(f"Sended notification to {user}")
 | 
				
			||||||
 | 
					        await bot.send_message(user, message, parse_mode='Markdown')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user