Skip to content

Commit dae543d

Browse files
committed
Remove global from FastAPI app.py
1 parent 846a334 commit dae543d

File tree

1 file changed

+12
-14
lines changed
  • airflow-core/src/airflow/api_fastapi

1 file changed

+12
-14
lines changed

airflow-core/src/airflow/api_fastapi/app.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import logging
2020
from contextlib import AsyncExitStack, asynccontextmanager
21+
from functools import cache
2122
from typing import TYPE_CHECKING, cast
2223
from urllib.parse import urlsplit
2324

@@ -54,8 +55,7 @@
5455

5556
log = logging.getLogger(__name__)
5657

57-
app: FastAPI | None = None
58-
auth_manager: BaseAuthManager | None = None
58+
_auth_manager: BaseAuthManager | None = None
5959

6060

6161
@asynccontextmanager
@@ -107,19 +107,17 @@ def create_app(apps: str = "all") -> FastAPI:
107107
return app
108108

109109

110+
@cache
110111
def cached_app(config=None, testing=False, apps="all") -> FastAPI:
111112
"""Return cached instance of Airflow API app."""
112-
global app
113-
if not app:
114-
app = create_app(apps=apps)
115-
return app
113+
return create_app(apps=apps)
116114

117115

118116
def purge_cached_app() -> None:
119117
"""Remove the cached version of the app and auth_manager in global state."""
120-
global app, auth_manager
121-
app = None
122-
auth_manager = None
118+
global _auth_manager
119+
cached_app.cache_clear()
120+
_auth_manager = None
123121

124122

125123
def get_auth_manager_cls() -> type[BaseAuthManager]:
@@ -140,10 +138,10 @@ def get_auth_manager_cls() -> type[BaseAuthManager]:
140138

141139
def create_auth_manager() -> BaseAuthManager:
142140
"""Create the auth manager."""
143-
global auth_manager
141+
global _auth_manager
144142
auth_manager_cls = get_auth_manager_cls()
145-
auth_manager = auth_manager_cls()
146-
return auth_manager
143+
_auth_manager = auth_manager_cls()
144+
return _auth_manager
147145

148146

149147
def init_auth_manager(app: FastAPI | None = None) -> BaseAuthManager:
@@ -161,12 +159,12 @@ def init_auth_manager(app: FastAPI | None = None) -> BaseAuthManager:
161159

162160
def get_auth_manager() -> BaseAuthManager:
163161
"""Return the auth manager, provided it's been initialized before."""
164-
if auth_manager is None:
162+
if _auth_manager is None:
165163
raise RuntimeError(
166164
"Auth Manager has not been initialized yet. "
167165
"The `init_auth_manager` method needs to be called first."
168166
)
169-
return auth_manager
167+
return _auth_manager
170168

171169

172170
def init_plugins(app: FastAPI) -> None:

0 commit comments

Comments
 (0)