diff --git a/inboard/gunicorn_conf.py b/inboard/gunicorn_conf.py index e1bf8b5..727e09d 100644 --- a/inboard/gunicorn_conf.py +++ b/inboard/gunicorn_conf.py @@ -3,13 +3,13 @@ from inboard.start import configure_logging -workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1") +# Gunicorn setup max_workers_str = os.getenv("MAX_WORKERS") +web_concurrency_str = os.getenv("WEB_CONCURRENCY", None) +workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1") use_max_workers = None -if max_workers_str: +if max_workers_str and int(max_workers_str) > 0: use_max_workers = int(max_workers_str) -web_concurrency_str = os.getenv("WEB_CONCURRENCY", None) - host = os.getenv("HOST", "0.0.0.0") port = os.getenv("PORT", "80") bind_env = os.getenv("BIND", None) @@ -18,9 +18,8 @@ cores = multiprocessing.cpu_count() workers_per_core = float(workers_per_core_str) default_web_concurrency = workers_per_core * cores -if web_concurrency_str: +if web_concurrency_str and int(web_concurrency_str) > 0: web_concurrency = int(web_concurrency_str) - assert web_concurrency > 0 else: web_concurrency = max(int(default_web_concurrency), 2) if use_max_workers: diff --git a/tests/test_start.py b/tests/test_start.py index 27d9eab..88bf975 100644 --- a/tests/test_start.py +++ b/tests/test_start.py @@ -393,6 +393,50 @@ def test_start_server_uvicorn_gunicorn( ) mock_logger.debug.assert_called_once_with("Running Uvicorn with Gunicorn.") # type: ignore[attr-defined] # noqa: E501 + @pytest.mark.parametrize( + "app_module", + [ + "inboard.app.base.main:app", + "inboard.app.fastapibase.main:app", + "inboard.app.starlettebase.main:app", + ], + ) + def test_start_server_uvicorn_gunicorn_custom_config( + self, + app_module: str, + gunicorn_conf_path: Path, + logging_conf_dict: Dict[str, Any], + mock_logger: logging.Logger, + mocker: MockerFixture, + monkeypatch: MonkeyPatch, + ) -> None: + """Test `start.start_server` with Uvicorn managed by Gunicorn.""" + assert os.getenv("GUNICORN_CONF", str(gunicorn_conf_path)) + monkeypatch.setenv("LOG_FORMAT", "gunicorn") + monkeypatch.setenv("LOG_LEVEL", "debug") + monkeypatch.setenv("MAX_WORKERS", "1") + monkeypatch.setenv("PROCESS_MANAGER", "gunicorn") + monkeypatch.setenv("WEB_CONCURRENCY", "4") + assert os.getenv("LOG_FORMAT") == "gunicorn" + assert os.getenv("LOG_LEVEL") == "debug" + assert os.getenv("MAX_WORKERS") == "1" + assert os.getenv("PROCESS_MANAGER") == "gunicorn" + assert os.getenv("WEB_CONCURRENCY") == "4" + start.start_server( + str(os.getenv("PROCESS_MANAGER")), + app_module=app_module, + logger=mock_logger, + logging_conf_dict=logging_conf_dict, + ) + monkeypatch.delenv("WEB_CONCURRENCY") + start.start_server( + str(os.getenv("PROCESS_MANAGER")), + app_module=app_module, + logger=mock_logger, + logging_conf_dict=logging_conf_dict, + ) + mock_logger.debug.assert_called_with("Running Uvicorn with Gunicorn.") # type: ignore[attr-defined] # noqa: E501 + def test_start_server_uvicorn_incorrect_module( self, logging_conf_dict: Dict[str, Any],