Skip to content

Commit c0c41ff

Browse files
authored
Respect apps flags for api_server_command (#52929)
* Respect apps flags for api_server_command * Refactor test_api_apps_env
1 parent 4cc8ebf commit c0c41ff

2 files changed

Lines changed: 81 additions & 38 deletions

File tree

‎airflow-core/src/airflow/cli/commands/api_server_command.py‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,27 @@
2323
import subprocess
2424
import sys
2525
import textwrap
26+
from collections.abc import Callable
27+
from functools import wraps
28+
from typing import TYPE_CHECKING, TypeVar
2629

2730
import uvicorn
2831

2932
from airflow import settings
3033
from airflow.cli.commands.daemon_utils import run_command_with_daemon_option
3134
from airflow.exceptions import AirflowConfigException
35+
from airflow.typing_compat import ParamSpec
3236
from airflow.utils import cli as cli_utils
3337
from airflow.utils.providers_configuration_loader import providers_configuration_loaded
3438

39+
PS = ParamSpec("PS")
40+
RT = TypeVar("RT")
41+
AIRFLOW_API_APPS = "AIRFLOW_API_APPS"
42+
3543
log = logging.getLogger(__name__)
3644

45+
if TYPE_CHECKING:
46+
from argparse import Namespace
3747

3848
# This shouldn't be necessary but there seems to be an issue in uvloop that causes bad file descriptor
3949
# errors when shutting down workers. Despite the 'closed' status of the issue it is not solved,
@@ -89,9 +99,32 @@ def _run_api_server(args, apps: str, num_workers: int, worker_timeout: int, prox
8999
)
90100

91101

102+
def with_api_apps_env(func: Callable[[Namespace], RT]) -> Callable[[Namespace], RT]:
103+
"""We use AIRFLOW_API_APPS to specify which apps are initialized in the API server."""
104+
105+
@wraps(func)
106+
def wrapper(args: Namespace) -> RT:
107+
apps: str = args.apps
108+
original_value = os.environ.get(AIRFLOW_API_APPS)
109+
try:
110+
log.debug("Setting AIRFLOW_API_APPS to: %s", apps)
111+
os.environ[AIRFLOW_API_APPS] = apps
112+
return func(args)
113+
finally:
114+
if original_value is not None:
115+
os.environ[AIRFLOW_API_APPS] = original_value
116+
log.debug("Restored AIRFLOW_API_APPS to: %s", original_value)
117+
else:
118+
os.environ.pop(AIRFLOW_API_APPS, None)
119+
log.debug("Removed AIRFLOW_API_APPS from environment")
120+
121+
return wrapper
122+
123+
92124
@cli_utils.action_cli
93125
@providers_configuration_loaded
94-
def api_server(args):
126+
@with_api_apps_env
127+
def api_server(args: Namespace):
95128
"""Start Airflow API server."""
96129
print(settings.HEADER)
97130

@@ -125,16 +158,11 @@ def api_server(args):
125158
if args.log_config and args.log_config != "-":
126159
run_args.extend(["--log-config", args.log_config])
127160

128-
# There is no way to pass the apps to airflow/api_fastapi/main.py in the development mode
129-
# because fastapi dev command does not accept any additional arguments
130-
# so environment variable is being used to pass it
131-
os.environ["AIRFLOW_API_APPS"] = apps
132161
with subprocess.Popen(
133162
run_args,
134163
close_fds=True,
135164
) as process:
136165
process.wait()
137-
os.environ.pop("AIRFLOW_API_APPS")
138166
else:
139167
run_command_with_daemon_option(
140168
args=args,

‎airflow-core/tests/unit/cli/commands/test_api_server_command.py‎

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -101,49 +101,64 @@ def test_dev_arg(self, args, expected_command):
101101
close_fds=True,
102102
)
103103

104-
def test_apps_env_var_set_unset(self):
104+
@pytest.mark.parametrize(
105+
"args",
106+
[
107+
(["api-server"]),
108+
(["api-server", "--apps", "all"]),
109+
(["api-server", "--apps", "core,execution"]),
110+
(["api-server", "--apps", "core"]),
111+
(["api-server", "--apps", "execution"]),
112+
],
113+
ids=[
114+
"default_apps",
115+
"all_apps_explicit",
116+
"multiple_apps_explicit",
117+
"single_app_core",
118+
"single_app_execution",
119+
],
120+
)
121+
@pytest.mark.parametrize("dev_mode", [True, False])
122+
@pytest.mark.parametrize(
123+
"original_env",
124+
[None, "some_value"],
125+
)
126+
def test_api_apps_env(self, args, dev_mode, original_env):
105127
"""
106128
Test that AIRFLOW_API_APPS is set and unset in the environment when
107129
calling the airflow api-server command
108130
"""
131+
expected_setitem_calls = []
132+
133+
if dev_mode:
134+
args.append("--dev")
135+
109136
with (
110-
mock.patch("subprocess.Popen") as Popen,
111137
mock.patch("os.environ", autospec=True) as mock_environ,
138+
mock.patch("uvicorn.run"),
139+
mock.patch("subprocess.Popen"),
112140
):
113-
apps_value = "core,execution"
114-
port = "9092"
115-
host = "somehost"
141+
# Mock the environment variable with initial value or None
142+
mock_environ.get.return_value = original_env
116143

117-
# Parse the command line arguments
118-
args = self.parser.parse_args(
119-
["api-server", "--port", port, "--host", host, "--apps", apps_value, "--dev"]
120-
)
121-
122-
# Ensure AIRFLOW_API_APPS is not set initially
123-
mock_environ.get.return_value = None
144+
# Parse the command line arguments and call the api_server command
145+
parsed_args = self.parser.parse_args(args)
146+
api_server_command.api_server(parsed_args)
124147

125-
# Call the fastapi_api command
126-
api_server_command.api_server(args)
127-
128-
# Assert that AIRFLOW_API_APPS was set in the environment before subprocess
129-
mock_environ.__setitem__.assert_called_with("AIRFLOW_API_APPS", apps_value)
148+
# Verify the AIRFLOW_API_APPS was set correctly
149+
if "--apps" in args:
150+
expected_setitem_calls.append(
151+
mock.call(api_server_command.AIRFLOW_API_APPS, parsed_args.apps)
152+
)
130153

131-
# Simulate subprocess execution
132-
Popen.assert_called_with(
133-
[
134-
"fastapi",
135-
"dev",
136-
"airflow-core/src/airflow/api_fastapi/main.py",
137-
"--port",
138-
port,
139-
"--host",
140-
host,
141-
],
142-
close_fds=True,
143-
)
154+
# Verify AIRFLOW_API_APPS was cleaned up
155+
if original_env is not None:
156+
expected_setitem_calls.append(mock.call(api_server_command.AIRFLOW_API_APPS, original_env))
157+
else:
158+
mock_environ.pop.assert_called_with(api_server_command.AIRFLOW_API_APPS, None)
144159

145-
# Assert that AIRFLOW_API_APPS was unset after subprocess
146-
mock_environ.pop.assert_called_with("AIRFLOW_API_APPS")
160+
# Verify that the environment variable was set and cleaned up correctly
161+
mock_environ.__setitem__.assert_has_calls(expected_setitem_calls)
147162

148163
@pytest.mark.parametrize(
149164
"cli_args, expected_additional_kwargs",

0 commit comments

Comments
 (0)