|
7 | 7 | from eoapi.stac.app import app |
8 | 8 | from eoapi.stac.config import PostgresSettings, Settings |
9 | 9 | from mangum import Mangum |
| 10 | +from snapshot_restore_py import register_after_restore, register_before_snapshot |
10 | 11 | from stac_fastapi.pgstac.db import connect_to_db |
11 | 12 |
|
12 | 13 | logging.getLogger("mangum.lifespan").setLevel(logging.ERROR) |
13 | 14 | logging.getLogger("mangum.http").setLevel(logging.ERROR) |
14 | 15 |
|
15 | 16 | settings = Settings() |
| 17 | +postgres_settings = PostgresSettings() |
| 18 | + |
| 19 | +_connection_initialized = False |
| 20 | + |
| 21 | + |
| 22 | +@register_before_snapshot |
| 23 | +def on_snapshot(): |
| 24 | + """ |
| 25 | + Runtime hook called by Lambda before taking a snapshot. |
| 26 | + We close database connections that shouldn't be in the snapshot. |
| 27 | + """ |
| 28 | + |
| 29 | + if hasattr(app, "state") and hasattr(app.state, "readpool") and app.state.readpool: |
| 30 | + try: |
| 31 | + app.state.readpool.close() |
| 32 | + app.state.readpool = None |
| 33 | + except Exception as e: |
| 34 | + print(f"SnapStart: Error closing database readpool: {e}") |
| 35 | + |
| 36 | + if ( |
| 37 | + hasattr(app, "state") |
| 38 | + and hasattr(app.state, "writepool") |
| 39 | + and app.state.writepool |
| 40 | + ): |
| 41 | + try: |
| 42 | + app.state.writepool.close() |
| 43 | + app.state.writepool = None |
| 44 | + except Exception as e: |
| 45 | + print(f"SnapStart: Error closing database writepool: {e}") |
| 46 | + |
| 47 | + return {"statusCode": 200} |
| 48 | + |
| 49 | + |
| 50 | +@register_after_restore |
| 51 | +def on_snap_restore(): |
| 52 | + """ |
| 53 | + Runtime hook called by Lambda after restoring from a snapshot. |
| 54 | + We recreate database connections that were closed before the snapshot. |
| 55 | + """ |
| 56 | + global _connection_initialized |
| 57 | + |
| 58 | + try: |
| 59 | + try: |
| 60 | + loop = asyncio.get_running_loop() |
| 61 | + except RuntimeError: |
| 62 | + loop = asyncio.new_event_loop() |
| 63 | + asyncio.set_event_loop(loop) |
| 64 | + |
| 65 | + if hasattr(app.state, "readpool") and app.state.readpool: |
| 66 | + try: |
| 67 | + app.state.readpool.close() |
| 68 | + except Exception as e: |
| 69 | + print(f"SnapStart: Error closing stale readpool: {e}") |
| 70 | + app.state.readpool = None |
| 71 | + |
| 72 | + if hasattr(app.state, "writepool") and app.state.writepool: |
| 73 | + try: |
| 74 | + app.state.writepool.close() |
| 75 | + except Exception as e: |
| 76 | + print(f"SnapStart: Error closing stale writepool: {e}") |
| 77 | + app.state.writepool = None |
| 78 | + |
| 79 | + loop.run_until_complete( |
| 80 | + connect_to_db( |
| 81 | + app, |
| 82 | + postgres_settings=postgres_settings, |
| 83 | + add_write_connection_pool=settings.enable_transaction, |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + _connection_initialized = True |
| 88 | + |
| 89 | + except Exception as e: |
| 90 | + print(f"SnapStart: Failed to initialize database connection: {e}") |
| 91 | + raise |
| 92 | + |
| 93 | + return {"statusCode": 200} |
16 | 94 |
|
17 | 95 |
|
18 | 96 | @app.on_event("startup") |
19 | 97 | async def startup_event() -> None: |
20 | 98 | """Connect to database on startup.""" |
21 | 99 | await connect_to_db( |
22 | 100 | app, |
23 | | - postgres_settings=PostgresSettings(), |
| 101 | + postgres_settings=postgres_settings, |
24 | 102 | add_write_connection_pool=settings.enable_transaction, |
25 | 103 | ) |
26 | 104 |
|
|
0 commit comments