From a8664b8f5443b1541d43641577a60a610ac49769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Baasch=20de=20Souza?= Date: Tue, 14 Nov 2023 12:55:34 -0300 Subject: [PATCH] Replace deprecated FastAPI.on_event with lifespan event --- src/jenkins_to_github_notify/app.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/jenkins_to_github_notify/app.py b/src/jenkins_to_github_notify/app.py index 854b523..97833b8 100644 --- a/src/jenkins_to_github_notify/app.py +++ b/src/jenkins_to_github_notify/app.py @@ -5,6 +5,8 @@ delegating to other modules the actual work. """ import logging +from contextlib import asynccontextmanager +from typing import AsyncIterator from dotenv import dotenv_values from fastapi import FastAPI @@ -14,13 +16,9 @@ from jenkins_to_github_notify.notify import validate_event from jenkins_to_github_notify.notify import validate_secret -app = FastAPI() -config: dict[str, str] = {} -logger = logging.getLogger("app") - -@app.on_event("startup") -async def startup_event() -> None: +@asynccontextmanager +async def lifespan(app: FastAPI) -> AsyncIterator[None]: """Fail early during startup in case any variable is missing.""" for key, value in dotenv_values(".env").items(): assert isinstance(value, str), f"Unexpected type in config value: {value!r} {type(value)}" @@ -29,6 +27,12 @@ async def startup_event() -> None: logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)-8s") logger.setLevel(logging.INFO) logger.info("Configuration validated.") + yield + + +app = FastAPI(lifespan=lifespan) +config: dict[str, str] = {} +logger = logging.getLogger("app") @app.get("/jobs/notify")