From aa67363dc6d1c8e35590835aeb7dbaa262973d91 Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Thu, 3 Aug 2023 17:57:38 +0200 Subject: [PATCH] feat!: update titiler version and `fix` lifespan --- lib/stac-api/runtime/src/handler.py | 9 ++++++++- lib/titiler-pgstac-api/runtime/requirements.txt | 4 ++-- lib/titiler-pgstac-api/runtime/src/handler.py | 16 +++++++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/stac-api/runtime/src/handler.py b/lib/stac-api/runtime/src/handler.py index b72e37b..dd79f0b 100644 --- a/lib/stac-api/runtime/src/handler.py +++ b/lib/stac-api/runtime/src/handler.py @@ -2,8 +2,15 @@ Handler for AWS Lambda. """ +import asyncio +import os from mangum import Mangum from .app import app -handler = Mangum(app) +handler = Mangum(app, lifespan="off") + + +if "AWS_EXECUTION_ENV" in os.environ: + loop = asyncio.get_event_loop() + loop.run_until_complete(app.router.startup()) diff --git a/lib/titiler-pgstac-api/runtime/requirements.txt b/lib/titiler-pgstac-api/runtime/requirements.txt index fde041c..0659895 100644 --- a/lib/titiler-pgstac-api/runtime/requirements.txt +++ b/lib/titiler-pgstac-api/runtime/requirements.txt @@ -1,3 +1,3 @@ -titiler.pgstac==0.3.3 +titiler.pgstac==0.5.1 boto3>=1.26.139 -psycopg[binary, pool] \ No newline at end of file +psycopg[binary, pool] diff --git a/lib/titiler-pgstac-api/runtime/src/handler.py b/lib/titiler-pgstac-api/runtime/src/handler.py index 729d732..34cf618 100644 --- a/lib/titiler-pgstac-api/runtime/src/handler.py +++ b/lib/titiler-pgstac-api/runtime/src/handler.py @@ -2,10 +2,12 @@ Handler for AWS Lambda. """ +import asyncio import os from mangum import Mangum from utils import get_secret_dict from titiler.pgstac.main import app +from titiler.pgstac.db import connect_to_db pgstac_secret_arn = os.environ["PGSTAC_SECRET_ARN"] @@ -20,4 +22,16 @@ } ) -handler = Mangum(app) + +@app.on_event("startup") +async def startup_event() -> None: + """Connect to database on startup.""" + await connect_to_db(app) + + +handler = Mangum(app, lifespan="off") + + +if "AWS_EXECUTION_ENV" in os.environ: + loop = asyncio.get_event_loop() + loop.run_until_complete(app.router.startup())