Create request in db

This commit is contained in:
Zoe Roux 2025-05-14 12:03:17 +02:00
parent bc2f4e6c13
commit 6cba0576e9
No known key found for this signature in database
3 changed files with 21 additions and 5 deletions

View File

@ -35,6 +35,7 @@ async def lifespan(_):
RequestProcessor(db, client, CompositeProvider(tmdb)) as processor, RequestProcessor(db, client, CompositeProvider(tmdb)) as processor,
get_db() as db, get_db() as db,
): ):
_ = asyncio.create_task(processor.process_all())
scanner = FsScanner(client, RequestCreator(db)) scanner = FsScanner(client, RequestCreator(db))
if is_master: if is_master:
_ = asyncio.create_task(scanner.monitor()) _ = asyncio.create_task(scanner.monitor())

View File

@ -62,7 +62,7 @@ class RequestProcessor:
async def __aenter__(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_all)
return self return self
async def __aexit__( async def __aexit__(
@ -71,7 +71,15 @@ class RequestProcessor:
exc_value: BaseException | None, exc_value: BaseException | None,
traceback: TracebackType | None, traceback: TracebackType | None,
): ):
await self._database.remove_listener("scanner_requests", self.process_request) await self._database.remove_listener("scanner_requests", self.process_all)
async def process_all(self):
found = True
while found:
try:
found = await self.process_request()
except Exception as e:
logger.error("Failed to process one of the metadata request", exc_info=e)
async def process_request(self): async def process_request(self):
cur = await self._database.fetchrow( cur = await self._database.fetchrow(
@ -80,11 +88,11 @@ class RequestProcessor:
scanner.requests scanner.requests
set set
status = 'running', status = 'running',
started_at = nom()::timestamptz started_at = now()::timestamptz
where where
pk in ( pk in (
select select
* pk
from from
scanner.requests scanner.requests
where where
@ -96,8 +104,9 @@ class RequestProcessor:
* *
""" """
) )
logger.warning("toto %s", cur)
if cur is None: if cur is None:
return return False
request = Request.model_validate(cur) request = Request.model_validate(cur)
logger.info(f"Starting to process {request.title}") logger.info(f"Starting to process {request.title}")
@ -127,6 +136,7 @@ class RequestProcessor:
""", """,
[request.pk], [request.pk],
) )
return True
async def _run_request(self, request: Request) -> Resource: async def _run_request(self, request: Request) -> Resource:
if request.kind == "movie": if request.kind == "movie":

View File

@ -1,6 +1,8 @@
from abc import ABCMeta from abc import ABCMeta
from collections.abc import Mapping
from typing import Annotated, Any, Callable, override from typing import Annotated, Any, Callable, override
from asyncpg import Record
from langcodes import Language as BaseLanguage from langcodes import Language as BaseLanguage
from pydantic import BaseModel, ConfigDict, GetJsonSchemaHandler from pydantic import BaseModel, ConfigDict, GetJsonSchemaHandler
from pydantic.alias_generators import to_camel from pydantic.alias_generators import to_camel
@ -34,6 +36,9 @@ class Model(BaseModel):
) )
Mapping.register(Record) # type: ignore
class _LanguagePydanticAnnotation: class _LanguagePydanticAnnotation:
@classmethod @classmethod
def __get_pydantic_core_schema__( def __get_pydantic_core_schema__(