Skip to content

Commit

Permalink
Add error handling for start.set_conf_path
Browse files Browse the repository at this point in the history
The `start.set_conf_path()` method will now raise a `FileNotFoundError`
if no file is found.
  • Loading branch information
br3ndonland committed Sep 6, 2020
1 parent f2b5419 commit 1c6a7ec
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,11 @@ ENV APP_MODULE="package.custom.module:api" WORKERS_PER_CORE="2"

### Gunicorn and Uvicorn

- `GUNICORN_CONF`: Path to a [Gunicorn configuration file](https://docs.gunicorn.org/en/latest/settings.html#config-file).
- `GUNICORN_CONF`: Path to a [Gunicorn configuration file](https://docs.gunicorn.org/en/latest/settings.html#config-file). The Gunicorn command-line accepts file paths instead of module paths.
- Default:
- `"/app/inboard/gunicorn_conf.py"` (the default file provided with the Docker image)
- Custom:
- `GUNICORN_CONF="/app/package/custom_gunicorn_conf.py"`
- Notes
- Feel free to use the [`gunicorn_conf.py`](./inboard/gunicorn_conf.py) from this repo as a starting point for your own custom configuration.
- `GUNICORN_CONF` accepts a file path, instead of a module path, because Gunicorn is typically only run from within Docker, where the file path will be predictable.
- `HOST`: Host IP address (inside of the container) where Gunicorn will listen for requests.
- Default: `"0.0.0.0"`
- Custom: _TODO_
Expand Down
9 changes: 5 additions & 4 deletions inboard/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

def set_conf_path(module_stem: str) -> str:
"""Set the path to a configuration file."""
conf_var = str(os.getenv(f"{module_stem.upper()}_CONF"))
if Path(conf_var).is_file():
conf_var = str(
os.getenv(f"{module_stem.upper()}_CONF", f"/app/inboard/{module_stem}_conf.py")
)
if conf_var and Path(conf_var).is_file():
conf_path = conf_var
elif Path(f"/app/inboard/{module_stem}_conf.py").is_file():
conf_path = f"/app/inboard/{module_stem}_conf.py"
else:
conf_path = f"/{module_stem}_conf.py"
os.environ[f"{module_stem.upper()}_CONF"] = conf_path
raise FileNotFoundError(f"Unable to find {conf_var}")
return conf_path


Expand Down
6 changes: 6 additions & 0 deletions tests/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def test_set_custom_conf_path_gunicorn(
assert "logging" not in str(gunicorn_conf_path_tmp)
assert start.set_conf_path("gunicorn") == str(gunicorn_conf_path_tmp)

def test_set_incorrect_conf_path(self, monkeypatch: MonkeyPatch) -> None:
"""Set path to non-existent file and raise an error."""
with pytest.raises(FileNotFoundError):
monkeypatch.setenv("GUNICORN_CONF", "/no/file/here")
start.set_conf_path("gunicorn")


class TestConfigureLogging:
"""Test logging configuration methods.
Expand Down

0 comments on commit 1c6a7ec

Please sign in to comment.