refactor: Web 服务器从 gunicorn 迁移到 uvicorn

This commit is contained in:
qzl
2026-03-06 17:55:01 +08:00
parent b6087fd195
commit 105e7849fe
12 changed files with 108 additions and 22 deletions
@@ -22,12 +22,18 @@ def test_taskiq_app_configures_logging_on_import(
sys.modules.pop("core.taskiq", None)
called = {"count": 0, "args": None}
banner_called = {"count": 0, "kwargs": None}
def _fake_configure_logging(*args: object, **__: object) -> None:
called["count"] += 1
called["args"] = args
def _fake_log_service_banner(**kwargs: object) -> None:
banner_called["count"] += 1
banner_called["kwargs"] = kwargs
monkeypatch.setattr("core.logging.configure_logging", _fake_configure_logging)
monkeypatch.setattr("core.logging.log_service_banner", _fake_log_service_banner)
importlib.import_module("core.taskiq.app")
@@ -35,3 +41,8 @@ def test_taskiq_app_configures_logging_on_import(
assert called["count"] == 1
assert called["args"] == (config,)
assert banner_called["count"] == 1
assert banner_called["kwargs"] == {
"service_name": config.runtime.service_name,
"environment": config.runtime.environment,
}
@@ -17,4 +17,17 @@ def test_worker_commands_use_taskiq() -> None:
assert "core.taskiq.app:bulk_broker" in content
assert 'pgrep -f "taskiq.*worker"' in content
assert 'pkill -f "taskiq.*worker"' in content
assert 'pgrep -f "gunicorn.*app:app"' in content
assert 'pkill -f "gunicorn.*app:app"' in content
assert removed_runner not in content
def test_web_command_uses_uvicorn_only() -> None:
content = APP_SCRIPT.read_text(encoding="utf-8")
assert "uv run uvicorn app:app" in content
assert 'WEB_PORT="${SOCIAL_WEB__PORT:-5775}"' in content
assert "SOCIAL_WEB__WORKERS" in content
assert 'UVICORN_LOG_LEVEL="${UVICORN_LOG_LEVEL,,}"' in content
assert "SOCIAL_WEB__GUNICORN__" not in content
assert "uv run gunicorn" not in content
+16
View File
@@ -71,6 +71,22 @@ def test_build_logging_config_plain_formatter_when_disabled(tmp_path: Path) -> N
assert handlers["error"]["formatter"] == "plain"
def test_build_logging_config_resolves_default_logs_from_project_root(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from core.config.settings import PROJECT_ROOT
monkeypatch.chdir(PROJECT_ROOT / "backend")
runtime = Settings().runtime
config = build_logging_config(runtime)
handlers = _get_handlers(config)
assert handlers["file"]["filename"] == str(PROJECT_ROOT / "logs" / "app.log")
assert handlers["error"]["filename"] == str(
PROJECT_ROOT / "logs" / "errors" / "app.error.log"
)
def _read_last_log_entry(log_path: Path) -> dict[str, object]:
assert log_path.exists(), f"Expected log file at {log_path}"
entries = [
+7 -1
View File
@@ -2,7 +2,7 @@ from __future__ import annotations
from pytest import MonkeyPatch
from core.config.settings import Settings
from core.config.settings import PROJECT_ROOT, Settings
def test_runtime_settings_defaults() -> None:
@@ -52,3 +52,9 @@ def test_runtime_settings_default_file_names_follow_service_name(
assert settings.runtime.log_error_dir == "logs/errors"
assert settings.runtime.log_file_name == "worker-default.log"
assert settings.runtime.log_error_file_name == "worker-default.error.log"
def test_project_root_points_to_repo_root() -> None:
assert (PROJECT_ROOT / "backend").is_dir()
assert (PROJECT_ROOT / "infra").is_dir()
assert (PROJECT_ROOT / "pyproject.toml").is_file()