fix: DB多重接続時に複数スレッドが出来上がってしまう問題の対処
This commit is contained in:
parent
54d4d09afa
commit
aa5bab9f27
@ -1,21 +0,0 @@
|
|||||||
"""FastAPIサーバーの起動・終了イベントのラッパー"""
|
|
||||||
|
|
||||||
from typing import Callable
|
|
||||||
|
|
||||||
from fastapi import FastAPI
|
|
||||||
|
|
||||||
from src.db.tasks import close_db, init_db
|
|
||||||
|
|
||||||
|
|
||||||
def create_start_app_handler(app: FastAPI) -> Callable:
|
|
||||||
def start_app() -> None:
|
|
||||||
init_db(app)
|
|
||||||
|
|
||||||
return start_app
|
|
||||||
|
|
||||||
|
|
||||||
def create_stop_app_handler(app: FastAPI) -> Callable:
|
|
||||||
def stop_app() -> None:
|
|
||||||
close_db(app)
|
|
||||||
|
|
||||||
return stop_app
|
|
||||||
@ -1,14 +0,0 @@
|
|||||||
from fastapi import FastAPI
|
|
||||||
|
|
||||||
from src.db.database import Database
|
|
||||||
|
|
||||||
|
|
||||||
def init_db(app: FastAPI) -> None:
|
|
||||||
# DB接続モジュールを初期化
|
|
||||||
database = Database.get_instance()
|
|
||||||
# FastAPI App内で使える変数として追加
|
|
||||||
app.state._db = database
|
|
||||||
|
|
||||||
|
|
||||||
def close_db(app: FastAPI) -> None:
|
|
||||||
app.state._db = None
|
|
||||||
@ -1,17 +1,8 @@
|
|||||||
from typing import Callable, Type
|
|
||||||
|
|
||||||
from fastapi import Depends
|
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
|
|
||||||
from src.db.database import Database
|
from src.db.database import Database
|
||||||
from src.repositories.base_repository import BaseRepository
|
|
||||||
|
|
||||||
|
|
||||||
def get_database(request: Request) -> Database:
|
def get_database(request: Request) -> Database:
|
||||||
|
# medaca_routerでDB接続エンジンが初期化される
|
||||||
return request.app.state._db
|
return request.app.state._db
|
||||||
|
|
||||||
|
|
||||||
def get_repository(Repo_type: Type[BaseRepository]) -> Callable:
|
|
||||||
def get_repo(db: Database = Depends(get_database)) -> Type[BaseRepository]:
|
|
||||||
return Repo_type(db)
|
|
||||||
return get_repo
|
|
||||||
|
|||||||
@ -7,7 +7,6 @@ from starlette import status
|
|||||||
import src.static as static
|
import src.static as static
|
||||||
from src.controller import (bio, bio_download, healthcheck, login, logout,
|
from src.controller import (bio, bio_download, healthcheck, login, logout,
|
||||||
master_mainte, menu, root, ultmarc)
|
master_mainte, menu, root, ultmarc)
|
||||||
from src.core import tasks
|
|
||||||
from src.error.exception_handler import http_exception_handler
|
from src.error.exception_handler import http_exception_handler
|
||||||
from src.error.exceptions import UnexpectedException
|
from src.error.exceptions import UnexpectedException
|
||||||
|
|
||||||
@ -41,7 +40,3 @@ app.add_exception_handler(status.HTTP_403_FORBIDDEN, http_exception_handler)
|
|||||||
|
|
||||||
# サーバーエラーが発生した場合のハンドラー。HTTPExceptionではハンドリングできないため、個別に設定
|
# サーバーエラーが発生した場合のハンドラー。HTTPExceptionではハンドリングできないため、個別に設定
|
||||||
app.add_exception_handler(UnexpectedException, http_exception_handler)
|
app.add_exception_handler(UnexpectedException, http_exception_handler)
|
||||||
|
|
||||||
# サーバー起動・終了イベントを登録
|
|
||||||
app.add_event_handler('startup', tasks.create_start_app_handler(app))
|
|
||||||
app.add_event_handler('shutdown', tasks.create_stop_app_handler(app))
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@ from fastapi.exceptions import HTTPException
|
|||||||
from fastapi.routing import APIRoute
|
from fastapi.routing import APIRoute
|
||||||
from starlette import status
|
from starlette import status
|
||||||
|
|
||||||
|
from src.db.database import Database
|
||||||
from src.depends.auth import (check_session_expired, get_current_session,
|
from src.depends.auth import (check_session_expired, get_current_session,
|
||||||
verify_session)
|
verify_session)
|
||||||
from src.error.exceptions import DBException, UnexpectedException
|
from src.error.exceptions import DBException, UnexpectedException
|
||||||
@ -75,6 +76,18 @@ class MeDaCaRoute(APIRoute):
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
class PrepareDatabaseRoute(MeDaCaRoute):
|
||||||
|
"""事前処理として、データベースのエンジンを作成するルートハンドラー
|
||||||
|
Args:
|
||||||
|
MeDaCaRoute (MeDaCaRoute): 共通ルートハンドラー
|
||||||
|
"""
|
||||||
|
async def pre_process_route(self, request: Request):
|
||||||
|
request = await super().pre_process_route(request)
|
||||||
|
# DBエンジンを構築して状態にセット
|
||||||
|
request.app.state._db = Database.get_instance()
|
||||||
|
return request
|
||||||
|
|
||||||
|
|
||||||
class BeforeCheckSessionRoute(MeDaCaRoute):
|
class BeforeCheckSessionRoute(MeDaCaRoute):
|
||||||
"""事前処理として、セッションチェックを行うルートハンドラー
|
"""事前処理として、セッションチェックを行うルートハンドラー
|
||||||
|
|
||||||
@ -97,11 +110,11 @@ class BeforeCheckSessionRoute(MeDaCaRoute):
|
|||||||
return session_request
|
return session_request
|
||||||
|
|
||||||
|
|
||||||
class AfterSetCookieSessionRoute(MeDaCaRoute):
|
class AfterSetCookieSessionRoute(PrepareDatabaseRoute):
|
||||||
"""事後処理として、セッションキーをcookieに設定するカスタムルートハンドラー
|
"""事後処理として、セッションキーをcookieに設定するカスタムルートハンドラー
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
MeDaCaRoute (MeDaCaRoute): 共通ルートハンドラー
|
PrepareDatabaseRoute (PrepareDatabaseRoute): DBチェックハンドラー
|
||||||
"""
|
"""
|
||||||
async def post_process_route(self, request: Request, response: Response):
|
async def post_process_route(self, request: Request, response: Response):
|
||||||
response = await super().post_process_route(request, response)
|
response = await super().post_process_route(request, response)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user