test: lazy initiation of catalogue
Some checks failed
PR tests / build-and-smoke (pull_request) Failing after 2m5s

This commit is contained in:
2025-11-25 09:15:54 +01:00
parent 0e7c45182a
commit af1ee0ce71

View File

@@ -1,5 +1,4 @@
""" """Lightweight Python API service for signature validation
Lightweight Python API service for signature validation
This can run independently to support the PHP application This can run independently to support the PHP application
""" """
@@ -22,15 +21,22 @@ app.add_middleware(
allow_headers=["*"], allow_headers=["*"],
) )
# Initialize catalogue for signature validation # Catalogue is expensive to initialize at import time; instantiate lazily
cat = catalogue.Catalogue() cat = None
def _get_catalogue():
global cat
if cat is None:
cat = catalogue.Catalogue()
return cat
@app.get("/api/validate-signature") @app.get("/api/validate-signature")
async def validate_signature(signature: str = Query(...)): async def validate_signature(signature: str = Query(...)):
"""Validate a book signature and return total pages""" """Validate a book signature and return total pages"""
try: try:
book_result = cat.get_book_with_data(signature) book_result = _get_catalogue().get_book_with_data(signature)
if book_result and hasattr(book_result, "pages") and book_result.pages: if book_result and hasattr(book_result, "pages") and book_result.pages:
# Try to extract numeric page count # Try to extract numeric page count
pages_str = str(book_result.pages) pages_str = str(book_result.pages)
@@ -39,7 +45,7 @@ async def validate_signature(signature: str = Query(...)):
if match: if match:
total_pages = int(match.group(1)) total_pages = int(match.group(1))
return JSONResponse( return JSONResponse(
{"valid": True, "total_pages": total_pages, "signature": signature} {"valid": True, "total_pages": total_pages, "signature": signature},
) )
return JSONResponse( return JSONResponse(
@@ -47,13 +53,13 @@ async def validate_signature(signature: str = Query(...)):
"valid": False, "valid": False,
"error": "Signatur nicht gefunden oder keine Seitenzahl verfügbar", "error": "Signatur nicht gefunden oder keine Seitenzahl verfügbar",
"signature": signature, "signature": signature,
} },
) )
except Exception as e: except Exception as e:
return JSONResponse( return JSONResponse(
{ {
"valid": False, "valid": False,
"error": f"Fehler bei der Validierung: {str(e)}", "error": f"Fehler bei der Validierung: {e!s}",
"signature": signature, "signature": signature,
}, },
status_code=500, status_code=500,