Close sessions for all services on shutdown

This commit is contained in:
Machka Pasla
2025-07-29 14:44:55 +03:00
parent 14f244ab79
commit 8246e9a590
4 changed files with 48 additions and 7 deletions
+9
View File
@@ -46,6 +46,15 @@ class CryptoPayService:
self.client = None
self.configured = False
async def close(self):
"""Close underlying AioCryptoPay session if initialized."""
if self.client:
try:
await self.client.close()
logging.info("CryptoPay client session closed.")
except Exception as e:
logging.warning(f"Failed to close CryptoPay client: {e}")
async def create_invoice(
self,
session: AsyncSession,
+4
View File
@@ -34,6 +34,10 @@ class PanelApiService:
self._session = None
logging.info("Panel API service HTTP session closed.")
async def close(self):
"""Alias for close_session for API consistency."""
await self.close_session()
async def _prepare_headers(self) -> Dict[str, str]:
headers = {
"Content-Type": "application/json",
+2 -2
View File
@@ -142,7 +142,7 @@ class YooKassaService:
f"Amount: {amount} {currency}. Metadata: {metadata}. Receipt: {receipt_data_dict}"
)
loop = asyncio.get_event_loop()
loop = asyncio.get_running_loop()
response = await loop.run_in_executor(
None, lambda: YooKassaPayment.create(payment_request,
idempotence_key))
@@ -196,7 +196,7 @@ class YooKassaService:
f"Fetching payment info from YooKassa for ID: {payment_id_in_yookassa}"
)
loop = asyncio.get_event_loop()
loop = asyncio.get_running_loop()
payment_info_yk = await loop.run_in_executor(
None, lambda: YooKassaPayment.find_one(payment_id_in_yookassa))