Detect the app is exiting and avoid reconnecting to computes.

This commit is contained in:
grossmj
2021-04-17 18:33:20 +09:30
parent 6b8ce8219c
commit bad3ef7003
4 changed files with 19 additions and 13 deletions

View File

@ -25,7 +25,7 @@ from fastapi import FastAPI, Request
from starlette.exceptions import HTTPException as StarletteHTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from uvicorn.main import Server as UvicornServer
from gns3server.controller.controller_error import (
ControllerError,
@ -82,6 +82,18 @@ def get_application() -> FastAPI:
app = get_application()
# Monkey Patch uvicorn signal handler to detect the application is shutting down
app.state.exiting = False
unicorn_exit_handler = UvicornServer.handle_exit
def handle_exit(*args, **kwargs):
app.state.exiting = True
unicorn_exit_handler(*args, **kwargs)
UvicornServer.handle_exit = handle_exit
@app.exception_handler(ControllerError)
async def controller_error_handler(request: Request, exc: ControllerError):