Use this skill when code must run during application startup/shutdown or when lifecycle orchestration is needed.
Workflow
- Register startup hooks for shared resources (DB pools, clients, caches).
- Register shutdown hooks for cleanup.
- Keep hooks idempotent and failure-aware.
- Use app state/dependencies to expose initialized resources.
Pattern
from litestar import Litestar
async def on_startup() -> None:
pass
async def on_shutdown() -> None: pass
app = Litestar( route_handlers=[], on_startup=[on_startup], on_shutdown=[on_shutdown], )
## Event Checklist
- Fail fast on critical startup failures.
- Ensure shutdown paths release external resources.
- Avoid long blocking tasks in lifecycle hooks.
## Litestar References
- https://docs.litestar.dev/latest/usage/events.html