async def lifespan(app: FastAPI):
    """Lifespan context manager for startup and shutdown events."""
    # Startup
    async with AsyncSessionLocal() as db:
        if settings.RUN_ALEMBIC_MIGRATIONS:
            logger.info("Running alembic migrations...")
            subprocess.run(["alembic", "upgrade", "head"], check=True)
        if settings.RUN_DB_SYNC:
            # Ensure all FileEntity subclasses have their parent and chunk models created
            ensure_file_entity_models()
            await sync_platform_components("airweave/platform", db)
        await init_db(db)

    # Start the sync scheduler
    await platform_scheduler.start()

    yield

    # Shutdown
    # Stop the sync scheduler
    await platform_scheduler.stop()
Lifespan context manager for startup and shutdown events.
backend/airweave/main.py