async def lifespan(app: FastAPI):
    """
    Manages the lifespan of the FastAPI application.

    This function is responsible for starting the training process and
    managing a background task runner. It logs the process ID of the
    training job, adds the job to the database, and ensures the background
    task is properly cancelled when the application shuts down.

    Args:
        app (FastAPI): The FastAPI application instance.

    Yields:
        None: This function is a generator that yields control back to the
        FastAPI application lifecycle.
    """
    process_pid = run_training(params=PARAMS, task_id=TASK_ID)
    logger.info(f"Started training with PID {process_pid}")
    DB.add_job(process_pid)
    task = asyncio.create_task(runner.run_main())
    yield

    task.cancel()
    try:
        await task
    except asyncio.CancelledError:
        logger.info("Background runner task cancelled.")

    Manages the lifespan of the FastAPI application.

    This function is responsible for starting the training process and
    managing a background task runner. It logs the process ID of the
    training job, adds the job to the database, and ensures the background
    task is properly cancelled when the application shuts down.

    Args:
        app (FastAPI): The FastAPI application instance.

    Yields:
        None: This function is a generator that yields control back to the
        FastAPI application lifecycle.
    
src/autotrain/app/training_api.py