Azure / azure-functions-python-library

Azure Functions Python SDK
MIT License
147 stars 61 forks source link

ASGI lifecycle events #187

Closed tonybaloney closed 8 months ago

tonybaloney commented 1 year ago

This PR calls the lifecycle event for startup and shutdown for an ASGI application provided by the new V2 wrapper.

This FastAPI application:

import fastapi
import logging

logger = logging.getLogger("azure.functions.AsgiMiddleware")

app = fastapi.FastAPI()

@app.get("/")
def index():
    logger.warning("Running primary route.")

    return {"message": "Hello, world?"}

@app.on_event("startup")
async def startup_event():
    print("Starting up...")
    logger.warning("Starting up ASGI app...")

@app.on_event("shutdown")
async def shutdown_event():
    print("Shutting down...")
    logger.warning("Shutting down ASGI app...")

And this function_app.py code:

import azure.functions as func
from fastapi_app import app as fa

app = func.AsgiFunctionApp(app=fa, http_auth_level=func.AuthLevel.ANONYMOUS)

Will run the startup and shutdown events for the Functions worker process.

screenshot 2023-07-18 at 14 33 38
tonybaloney commented 12 months ago

Equivalent demo with quart

from quart import Quart

app = Quart(__name__)

@app.route('/')
async def hello():
    return 'hello'

@app.before_serving
async def create_db_pool():
    print("running quart startup 1")

@app.before_serving
async def use_g():
    print("Run quart startup 2")

@app.while_serving
async def lifespan():
    print("Initial startup")
    yield
    print("Shutting down quart")

@app.after_serving
async def create_db_pool():
    print("running quart shutdown 1")
codecov[bot] commented 12 months ago

Codecov Report

Attention: 17 lines in your changes are missing coverage. Please review.

Comparison is base (b42035a) 93.35% compared to head (f083cf8) 92.97%.

Files Patch % Lines
azure/functions/_http_asgi.py 83.87% 3 Missing and 7 partials :warning:
azure/functions/decorators/function_app.py 41.66% 6 Missing and 1 partial :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## dev #187 +/- ## ========================================== - Coverage 93.35% 92.97% -0.39% ========================================== Files 56 56 Lines 3116 3187 +71 Branches 633 649 +16 ========================================== + Hits 2909 2963 +54 - Misses 128 137 +9 - Partials 79 87 +8 ``` | [Flag](https://app.codecov.io/gh/Azure/azure-functions-python-library/pull/187/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/Azure/azure-functions-python-library/pull/187/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure) | `92.94% <77.02%> (-0.39%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Azure#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

tonybaloney commented 12 months ago
screenshot 2023-07-25 at 13 30 30

If a startup event fails, it will capture the error in the logs, fail the request and retry on the next request. The exception is not reported to the HTTP result from the initiating request.