diff --git a/roles/web-app-discourse/README.md b/roles/web-app-discourse/README.md index 823a2aa3a4..6c4d74372b 100644 --- a/roles/web-app-discourse/README.md +++ b/roles/web-app-discourse/README.md @@ -41,6 +41,10 @@ The bridged partners are co-deployed by variant 1 of [`meta/variants.yml`](./met The `ldap` service block in [`meta/services.yml`](./meta/services.yml) is intentionally pinned to literal `false` (see [TODO.md](./TODO.md)): the `jonmbake/discourse-ldap-auth` plugin breaks Discourse bootstrap on recent versions. `discourse-ldap-auth` therefore resolves to disabled until that block is flipped back to the dynamic group-membership form. +## Web workers + +`UNICORN_WORKERS` follows the host the same way in production and CI: two per vCPU as upstream's [setup wizard](https://github.com/discourse/discourse_docker/blob/775903891a8c34e87de66f747bd98ea48fa32eff/image/setup_wizard/lib/discourse_setup/resource_scaler.rb) sizes a dedicated host, but at most one per 4 GiB of host RAM, because the host is shared with the other applications and every web worker holds about 500 MiB. The count stays between 2 and upstream's ceiling of 8: a 4 vCPU runner with 16 GB gets 3, a host with at least 4 vCPU and 32 GiB visible RAM gets 8. Sidekiq keeps upstream's single process (`UNICORN_SIDEKIQS: 1`). + ## Further Resources - [Discourse Official Website](https://www.discourse.org/) diff --git a/roles/web-app-discourse/templates/config.yml.j2 b/roles/web-app-discourse/templates/config.yml.j2 index c439a24c54..242dc9347c 100644 --- a/roles/web-app-discourse/templates/config.yml.j2 +++ b/roles/web-app-discourse/templates/config.yml.j2 @@ -42,7 +42,7 @@ env: ## How many concurrent web requests are supported? Depends on memory and CPU cores. ## will be set automatically by bootstrap based on detected CPUs, or you can override - UNICORN_WORKERS: 8 + UNICORN_WORKERS: "{{ DISCOURSE_UNICORN_WORKERS }}" ## Required. Discourse will not work with a bare IP number. DISCOURSE_HOSTNAME: {{ lookup('domain',application_id) }} diff --git a/roles/web-app-discourse/vars/main.yml b/roles/web-app-discourse/vars/main.yml index a077abde77..c8c1f7d947 100644 --- a/roles/web-app-discourse/vars/main.yml +++ b/roles/web-app-discourse/vars/main.yml @@ -18,6 +18,7 @@ DISCOURSE_REDIS_PASSWORD: "{{ lookup('engine', 'redis', application_id, 'passwor DISCOURSE_REPOSITORY_URL: "{{ lookup('config', application_id, 'services.' ~ entity_name ~ '.repository') }}" DISCOURSE_REF: "{{ lookup('config', application_id, 'services.' ~ entity_name ~ '.ref') }}" DISCOURSE_VERSION: "{{ lookup('config', application_id, 'services.' ~ entity_name ~ '.application.ref') }}" +DISCOURSE_UNICORN_WORKERS: "{{ [[[2 * (RESOURCE_HOST_CPUS | int), (lookup('resource', 'host_mem_mb') | int) // 4096] | min, 2] | max, 8] | min }}" DISCOURSE_DATABASE: "{{ lookup('database', application_id ) }}" DISCOURSE_DATABASE_TYPE: "{{ DISCOURSE_DATABASE.type }}" diff --git a/tests/unit/python/roles/web-app-discourse/test_unicorn_workers.py b/tests/unit/python/roles/web-app-discourse/test_unicorn_workers.py new file mode 100644 index 0000000000..ce1e44f022 --- /dev/null +++ b/tests/unit/python/roles/web-app-discourse/test_unicorn_workers.py @@ -0,0 +1,70 @@ +from __future__ import annotations + +import re +import unittest + +from jinja2 import Environment, StrictUndefined, select_autoescape +from jinja2.nativetypes import NativeEnvironment + +from utils.cache.files import PROJECT_ROOT, read_text +from utils.cache.yaml import load_yaml_any, load_yaml_str +from utils.roles.mapping import ROLE_FILE_VARS_MAIN + +ROLE = PROJECT_ROOT / "roles" / "web-app-discourse" +VARS = ROLE / ROLE_FILE_VARS_MAIN +TEMPLATE = ROLE / "templates" / "config.yml.j2" +_ENV_LINE = re.compile(r"^ UNICORN_WORKERS:.*$", re.MULTILINE) + + +def _workers(cpus: int, mem_mb: int) -> int: + """Render ``env.UNICORN_WORKERS`` of the launcher config for one host. + + Args: + cpus: the host's vCPUs, as ``RESOURCE_HOST_CPUS`` carries them + mem_mb: the host's RAM in MiB, as ``lookup('resource', 'host_mem_mb')`` + returns it + + Returns: + the worker count the launcher hands the container + """ + + def lookup(kind: str, key: str) -> int: + assert (kind, key) == ("resource", "host_mem_mb"), (kind, key) + return mem_mb + + expression = load_yaml_any(str(VARS))["DISCOURSE_UNICORN_WORKERS"] + derived = ( + NativeEnvironment(undefined=StrictUndefined) + .from_string(expression) + .render(RESOURCE_HOST_CPUS=str(cpus), lookup=lookup) + ) + line = _ENV_LINE.search(read_text(str(TEMPLATE))) + assert line is not None, f"{TEMPLATE} sets no UNICORN_WORKERS" + environment = Environment( + undefined=StrictUndefined, + autoescape=select_autoescape(default_for_string=False), + ) + rendered = environment.from_string(line[0]).render( + DISCOURSE_UNICORN_WORKERS=derived + ) + return int(load_yaml_str(rendered)["UNICORN_WORKERS"]) + + +class TestUnicornWorkers(unittest.TestCase): + def test_the_count_follows_cpus_and_ram_of_the_host(self) -> None: + for cpus, mem_mb, expected in ( + (4, 15_983, 3), + (2, 6_932, 2), + (2, 65_536, 4), + (8, 32_768, 8), + (8, 31_744, 7), + (16, 65_536, 8), + (64, 262_144, 8), + (1, 1_024, 2), + ): + with self.subTest(cpus=cpus, mem_mb=mem_mb): + self.assertEqual(_workers(cpus, mem_mb), expected) + + +if __name__ == "__main__": + unittest.main()