Proper app cleanup

This commit is contained in:
Zoe Roux 2025-05-12 13:06:18 +02:00
parent decb09ecca
commit ffd4fb2fa6
No known key found for this signature in database
2 changed files with 13 additions and 3 deletions

View File

@ -25,9 +25,9 @@ async def lifespan(_):
get_db() as db, get_db() as db,
KyooClient() as client, KyooClient() as client,
TheMovieDatabase() as tmdb, TheMovieDatabase() as tmdb,
RequestProcessor(db, client, CompositeProvider(tmdb)) as processor
): ):
processor = RequestProcessor(db, client, CompositeProvider(tmdb)) # creating the processor makes it listen to requests event in pg
await processor.listen_for_requests()
async with ( async with (
get_db() as db, get_db() as db,
KyooClient() as client, KyooClient() as client,

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
from logging import getLogger from logging import getLogger
from types import TracebackType
from typing import Annotated, Literal from typing import Annotated, Literal
from asyncpg import Connection from asyncpg import Connection
@ -61,9 +62,18 @@ class RequestProcessor:
self._client = client self._client = client
self._providers = providers self._providers = providers
async def listen_for_requests(self): async def __aenter__(self):
logger.info("Listening for requestes") logger.info("Listening for requestes")
await self._database.add_listener("scanner.requests", self.process_request) await self._database.add_listener("scanner.requests", self.process_request)
return self
async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
):
await self._database.remove_listener("scanner.requests", self.process_request)
async def process_request(self): async def process_request(self):
cur = await self._database.fetchrow( cur = await self._database.fetchrow(