Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/stac-api/runtime/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ description = "stac-api runtime"
authors = [{ name = "hrodmn", email = "henry@developmentseed.org" }]
requires-python = ">=3.12"
dependencies = [
"mangum==0.19",
"stac-fastapi-pgstac[awslambda]>=6.0,<6.1",
"starlette-cramjam>=0.4,<0.5",
]
Expand Down
47 changes: 32 additions & 15 deletions lib/stac-api/runtime/src/stac_api/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import asyncio
import logging
import os

from mangum import Mangum
Expand All @@ -12,14 +13,21 @@
from stac_fastapi.pgstac.db import close_db_connection, connect_to_db
from utils import get_secret_dict

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)


logger.info("fetching pgstac secret")
secret = get_secret_dict(secret_arn_env_var="PGSTAC_SECRET_ARN")
postgres_settings = PostgresSettings(
postgres_host_reader=secret["host"],
postgres_host_writer=secret["host"],
postgres_dbname=secret["dbname"],
postgres_user=secret["username"],
postgres_pass=secret["password"],
postgres_port=int(secret["port"]),
pghost=secret["host"],
pgdatabase=secret["dbname"],
pguser=secret["username"],
pgpassword=secret["password"],
pgport=int(secret["port"]),
)

_connection_initialized = False
Expand All @@ -38,14 +46,14 @@ def on_snapshot():
app.state.readpool.close()
app.state.readpool = None
except Exception as e:
print(f"SnapStart: Error closing database readpool: {e}")
logger.info(f"SnapStart: Error closing database readpool: {e}")

if hasattr(app, "state") and hasattr(app.state, "writepool") and app.state.writepool:
try:
app.state.writepool.close()
app.state.writepool = None
except Exception as e:
print(f"SnapStart: Error closing database writepool: {e}")
logger.info(f"SnapStart: Error closing database writepool: {e}")

return {"statusCode": 200}

Expand All @@ -71,14 +79,14 @@ def on_snap_restore():
try:
app.state.readpool.close()
except Exception as e:
print(f"SnapStart: Error closing stale readpool: {e}")
logger.info(f"SnapStart: Error closing stale readpool: {e}")
app.state.readpool = None

if hasattr(app.state, "writepool") and app.state.writepool:
try:
app.state.writepool.close()
except Exception as e:
print(f"SnapStart: Error closing stale writepool: {e}")
logger.info(f"SnapStart: Error closing stale writepool: {e}")
app.state.writepool = None

# Create fresh connection pool
Expand All @@ -93,7 +101,7 @@ def on_snap_restore():
_connection_initialized = True

except Exception as e:
print(f"SnapStart: Failed to initialize database connection: {e}")
logger.error(f"SnapStart: Failed to initialize database connection: {e}")
raise

return {"statusCode": 200}
Expand All @@ -102,13 +110,13 @@ def on_snap_restore():
@app.on_event("startup")
async def startup_event():
"""Connect to database on startup."""
print("Setting up DB connection...")
logger.info("Setting up DB connection...")
await connect_to_db(
app,
postgres_settings=postgres_settings,
add_write_connection_pool=with_transactions,
)
print("DB connection setup.")
logger.info("DB connection setup.")


@app.on_event("shutdown")
Expand All @@ -131,5 +139,14 @@ async def shutdown_event():


if "AWS_EXECUTION_ENV" in os.environ:
loop = asyncio.get_event_loop()
loop.run_until_complete(app.router.startup())
logger.info("Cold start: initializing database connection...")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
connect_to_db(
app,
postgres_settings=postgres_settings,
add_write_connection_pool=with_transactions,
)
)
logger.info("Database connection initialized.")
632 changes: 311 additions & 321 deletions lib/stac-api/runtime/uv.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/stac-auth-proxy/runtime/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.0.0"
description = "stac-auth-proxy-api runtime"
requires-python = ">=3.12"
dependencies = [
"mangum>=0.19.0",
"mangum==0.19",
"stac-auth-proxy>=0.6,<1",
]

Expand Down
362 changes: 167 additions & 195 deletions lib/stac-auth-proxy/runtime/uv.lock

Large diffs are not rendered by default.

488 changes: 249 additions & 239 deletions lib/stac-loader/runtime/uv.lock

Large diffs are not rendered by default.

134 changes: 69 additions & 65 deletions lib/stactools-item-generator/runtime/uv.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/tipg-api/runtime/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.0.0"
description = "tipg-api runtime"
requires-python = ">=3.12"
dependencies = [
"mangum>=0.19.0",
"mangum==0.19",
"tipg>=1.2,<1.3",
]

Expand Down
494 changes: 257 additions & 237 deletions lib/tipg-api/runtime/uv.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/titiler-pgstac-api/runtime/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "titiler-pgstac-api runtime"
requires-python = ">=3.12"
dependencies = [
"titiler-pgstac[psycopg-binary]>=1.9.0,<1.10",
"mangum>=0.19.0",
"mangum==0.19",
]

[dependency-groups]
Expand Down
Loading
Loading