From 9d2f6c10a7de169a46042cdd9dedbadebd44bcf0 Mon Sep 17 00:00:00 2001 From: albertkun Date: Mon, 20 Nov 2023 15:26:26 -0800 Subject: [PATCH] refactor: gunicorn process feat: added traceback in event of error --- fastapi/Dockerfile | 2 +- fastapi/app/main.py | 58 +++++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/fastapi/Dockerfile b/fastapi/Dockerfile index fb59802..1570eb9 100644 --- a/fastapi/Dockerfile +++ b/fastapi/Dockerfile @@ -46,6 +46,6 @@ print(f'export DB_NAME={result.path[1:]}')" > /tmp/env.sh && \ echo "auth_file = /etc/pgbouncer/userlist.txt" >> /etc/pgbouncer/pgbouncer.ini && \ echo "client_login_timeout = 120" >> /etc/pgbouncer/pgbouncer.ini && \ gosu pgbouncer pgbouncer /etc/pgbouncer/pgbouncer.ini & \ - gunicorn app.main:app --workers 2 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:80 --timeout 120 --keep-alive 5 + gunicorn -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:80 app.main:app EXPOSE 80 \ No newline at end of file diff --git a/fastapi/app/main.py b/fastapi/app/main.py index 882548f..53991d9 100644 --- a/fastapi/app/main.py +++ b/fastapi/app/main.py @@ -2,6 +2,8 @@ from distutils.command.config import config from typing import Union, List, Dict, Optional from versiontag import get_version +import traceback +import sys import http import json @@ -738,32 +740,36 @@ async def get_all_routes(): @app.on_event("startup") async def startup_event(): - redis_pool = await aioredis.from_url(Config.REDIS_URL) - redis = RedisBackend(redis_pool) - FastAPICache.init(backend=redis, prefix="fastapi-cache") - uvicorn_access_logger = logging.getLogger("uvicorn.access") - uvicorn_error_logger = logging.getLogger("uvicorn.error") - logger = logging.getLogger("uvicorn.app") - logzio_formatter = logging.Formatter("%(message)s") - logzio_uvicorn_access_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'uvicorn.access', 5, Config.LOGZIO_URL) - logzio_uvicorn_access_handler.setLevel(logging.INFO) - logzio_uvicorn_access_handler.setFormatter(logzio_formatter) - - logzio_uvicorn_error_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'uvicorn.error', 5, Config.LOGZIO_URL) - logzio_uvicorn_error_handler.setLevel(logging.INFO) - logzio_uvicorn_error_handler.setFormatter(logzio_formatter) - - logzio_app_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'fastapi.app', 5, Config.LOGZIO_URL) - logzio_app_handler.setLevel(logging.INFO) - logzio_app_handler.setFormatter(logzio_formatter) - - uvicorn_access_logger.addHandler(logzio_uvicorn_access_handler) - uvicorn_error_logger.addHandler(logzio_uvicorn_error_handler) - logger.addHandler(logzio_app_handler) - - uvicorn_access_logger.addFilter(LogFilter()) - uvicorn_error_logger.addFilter(LogFilter()) - logger.addFilter(LogFilter()) + try: + redis_pool = await aioredis.from_url(Config.REDIS_URL) + redis = RedisBackend(redis_pool) + FastAPICache.init(backend=redis, prefix="fastapi-cache") + uvicorn_access_logger = logging.getLogger("uvicorn.access") + uvicorn_error_logger = logging.getLogger("uvicorn.error") + logger = logging.getLogger("uvicorn.app") + logzio_formatter = logging.Formatter("%(message)s") + logzio_uvicorn_access_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'uvicorn.access', 5, Config.LOGZIO_URL) + logzio_uvicorn_access_handler.setLevel(logging.INFO) + logzio_uvicorn_access_handler.setFormatter(logzio_formatter) + + logzio_uvicorn_error_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'uvicorn.error', 5, Config.LOGZIO_URL) + logzio_uvicorn_error_handler.setLevel(logging.INFO) + logzio_uvicorn_error_handler.setFormatter(logzio_formatter) + + logzio_app_handler = LogzioHandler(Config.LOGZIO_TOKEN, 'fastapi.app', 5, Config.LOGZIO_URL) + logzio_app_handler.setLevel(logging.INFO) + logzio_app_handler.setFormatter(logzio_formatter) + + uvicorn_access_logger.addHandler(logzio_uvicorn_access_handler) + uvicorn_error_logger.addHandler(logzio_uvicorn_error_handler) + logger.addHandler(logzio_app_handler) + + uvicorn_access_logger.addFilter(LogFilter()) + uvicorn_error_logger.addFilter(LogFilter()) + logger.addFilter(LogFilter()) + except: + exc_type, exc_value, exc_traceback = sys.exc_info() + traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stderr) app.add_middleware( CORSMiddleware,