From d72a1807646c800a51e23d998a141f6e9972a792 Mon Sep 17 00:00:00 2001 From: tomahawkeer Date: Mon, 2 Nov 2026 13:54:07 -0500 Subject: [PATCH] Read WORKERS in the systemd REST unit relaysms-publisher-rest.service hard-coded --workers 4, while scripts/run.sh and the Docker entrypoint read WORKERS from the environment, so setting WORKERS in .env had no effect on server installs. systemd's ExecStart= supports ${VAR} but not shell-style ${VAR:-4}, so the unit sets Environment=WORKERS=4 as the default and passes --workers "${WORKERS}". EnvironmentFile= overrides Environment=, so a WORKERS line in .env takes precedence. Adds a render_units test and notes in the settings reference that all three launchers read WORKERS from .env. Fixes #221 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FiGQujBCL8qnuH6bAv3Yiz --- deploy/systemd/relaysms-publisher-rest.service | 5 ++++- docs/configuration/settings.md | 2 +- tests/scripts/test_lib.py | 10 ++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/deploy/systemd/relaysms-publisher-rest.service b/deploy/systemd/relaysms-publisher-rest.service index ba34b62..3f5e0ae 100644 --- a/deploy/systemd/relaysms-publisher-rest.service +++ b/deploy/systemd/relaysms-publisher-rest.service @@ -10,12 +10,15 @@ StartLimitBurst=5 Type=simple User=relaysms WorkingDirectory=/opt/relaysms/relaysms-publisher +# Default worker count. EnvironmentFile= overrides Environment=, so WORKERS +# in .env wins. systemd has no ${VAR:-default} syntax, hence this line. +Environment=WORKERS=4 EnvironmentFile=/opt/relaysms/relaysms-publisher/.env Environment=OTEL_SERVICE_NAME=relaysms-publisher-rest Environment=PATH=/opt/relaysms/relaysms-publisher/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin # Fails the unit on a config error before uvicorn starts its workers. ExecStartPre=/opt/relaysms/relaysms-publisher/venv/bin/python3 -c "import publisher.api.rest.app" -ExecStart=/opt/relaysms/relaysms-publisher/scripts/otel-wrap.sh /opt/relaysms/relaysms-publisher/venv/bin/python3 -m uvicorn publisher.api.rest.app:app --host "${HOST}" --port "${PORT}" --workers 4 --proxy-headers --forwarded-allow-ips "*" +ExecStart=/opt/relaysms/relaysms-publisher/scripts/otel-wrap.sh /opt/relaysms/relaysms-publisher/venv/bin/python3 -m uvicorn publisher.api.rest.app:app --host "${HOST}" --port "${PORT}" --workers "${WORKERS}" --proxy-headers --forwarded-allow-ips "*" Restart=on-failure RestartSec=5 SyslogIdentifier=relaysms-publisher-rest diff --git a/docs/configuration/settings.md b/docs/configuration/settings.md index debec4b..6deb9d3 100644 --- a/docs/configuration/settings.md +++ b/docs/configuration/settings.md @@ -13,7 +13,7 @@ Settings live in `.env` in the install directory. `template.env` lists each one | --- | --- | --- | | `HOST` | `127.0.0.1` | REST API address. Docker always binds `0.0.0.0`. | | `PORT` | `16000` | REST API port | -| `WORKERS` | None | REST API worker processes: `1` by default under `scripts/run.sh`, `4` on Docker and server installs | +| `WORKERS` | None | REST API worker processes: `1` by default under `scripts/run.sh`, `4` on Docker and server installs. All three read it from `.env`. | | `GRPC_HOST` | `127.0.0.1` | gRPC address. Docker always binds `0.0.0.0`. | | `GRPC_PORT` | `6000` | gRPC port | | `GRPC_MAX_WORKERS` | `10` | gRPC worker threads | diff --git a/tests/scripts/test_lib.py b/tests/scripts/test_lib.py index 36aea53..87cb016 100644 --- a/tests/scripts/test_lib.py +++ b/tests/scripts/test_lib.py @@ -132,6 +132,16 @@ def test_renders_every_unit_for_this_install(self, install, bash): assert unit_values(rest, "ReadWritePaths") == [" ".join(app_directories(bash))] assert "/opt/relaysms" not in "".join(units.values()) + def test_rest_unit_reads_workers_with_a_default(self, install, bash): + rest = self.render(install, bash)["relaysms-publisher-rest.service"] + + # EnvironmentFile= overrides Environment=, so WORKERS in .env wins + # over this default. + assert unit_values(rest, "Environment").count("WORKERS=4") == 1 + assert unit_values(rest, "EnvironmentFile") + (exec_start,) = unit_values(rest, "ExecStart") + assert '--workers "${WORKERS}"' in exec_start + def test_named_instance_renames_units_and_references(self, bash, install): units = self.render(install, bash, INSTANCE_NAME="acme")