diff --git a/roles/web-app-nextcloud/meta/services.yml b/roles/web-app-nextcloud/meta/services.yml index bab4709cd9..5fd355f302 100644 --- a/roles/web-app-nextcloud/meta/services.yml +++ b/roles/web-app-nextcloud/meta/services.yml @@ -188,6 +188,12 @@ cron: mem_limit: 512m pids_limit: 256 min_storage: 1GB +taskprocessing: + name: nextcloud-taskprocessing + cpus: "0.5" + mem_reservation: 128m + mem_limit: 1g + pids_limit: 256 # nocheck: dynamic-flag # nocheck: playwright-service-flag: infrastructural sub-component (real-time communication) talk: diff --git a/roles/web-app-nextcloud/meta/volumes.yml b/roles/web-app-nextcloud/meta/volumes.yml index 599520cdcf..71472549cb 100644 --- a/roles/web-app-nextcloud/meta/volumes.yml +++ b/roles/web-app-nextcloud/meta/volumes.yml @@ -7,6 +7,8 @@ data: target: &var_www_html_target /var/www/html/ - service: cron target: *var_www_html_target + - service: taskprocessing + target: *var_www_html_target - service: proxy target: *var_www_html_target read_only: true @@ -60,6 +62,8 @@ infinito_conf: target: &var_www_infinito_target /var/www/infinito/ - service: cron target: *var_www_infinito_target + - service: taskprocessing + target: *var_www_infinito_target janus_conf: type: config diff --git a/roles/web-app-nextcloud/templates/compose.yml.j2 b/roles/web-app-nextcloud/templates/compose.yml.j2 index 3d17c31bd4..bdc5da2bf0 100644 --- a/roles/web-app-nextcloud/templates/compose.yml.j2 +++ b/roles/web-app-nextcloud/templates/compose.yml.j2 @@ -151,6 +151,31 @@ {{ lookup('container_depends_on', application_id) | indent(4) }} {{ lookup('container_networks', provider_self_alias=False) | indent(4) }} + {% set service_name = NEXTCLOUD_TASKPROCESSING_SERVICE %} + {{ service_name }}: + {% include 'roles/sys-svc-container/templates/deploy.yml.j2' %} + {{ lookup('compose_only', 'container_name', NEXTCLOUD_TASKPROCESSING_CONTAINER) }} + {{ lookup('container_image', application_id, 'nextcloud') }} + {{ lookup('compose_restart') }} + logging: + driver: journald + {{ lookup('container_volumes', service_name) | indent(4) }} + entrypoint: ["su", "{{ NEXTCLOUD_DOCKER_USER }}", "-s", "/bin/sh", "-c", "while true; do php {{ NEXTCLOUD_INTERNAL_OCC_COMMAND }} taskprocessing:worker -t 60 || sleep 10; done"] + sysctls: + net.ipv6.conf.all.disable_ipv6: "{{ (not NETWORK_IPV6_ENABLED | bool) | int }}" + healthcheck: + test: ["CMD", "su", "{{ NEXTCLOUD_DOCKER_USER }}", "-s", "/bin/sh", "-c", "php {{ NEXTCLOUD_INTERNAL_OCC_COMMAND }} status"] + # nocheck: healthcheck-timing-override occ boots the whole Nextcloud stack per probe, so it stays off a 30s loop + interval: 1m + # nocheck: healthcheck-timing-override occ regularly needs more than the 5s default to answer + timeout: 30s + retries: 10 + # nocheck: healthcheck-timing-override a role variable the linter cannot compare, tuned per deployment size + start_period: {{ container_healthcheck_start_period }} + start_interval: 5s + {{ lookup('container_depends_on', application_id) | indent(4) }} + {{ lookup('container_networks', provider_self_alias=False) | indent(4) }} + {% if NEXTCLOUD_RECORDING_ENABLED | bool %} {% set service_name = 'talk_recording' %} {{ service_name }}: diff --git a/roles/web-app-nextcloud/vars/main.yml b/roles/web-app-nextcloud/vars/main.yml index e30e3c6673..e480248c67 100644 --- a/roles/web-app-nextcloud/vars/main.yml +++ b/roles/web-app-nextcloud/vars/main.yml @@ -148,6 +148,9 @@ NEXTCLOUD_CONTEXT_AGENT_JSON_INFO: >- NEXTCLOUD_CRON_SERVICE: "cron" NEXTCLOUD_CRON_CONTAINER: "{{ lookup('config', application_id, 'services.' ~ NEXTCLOUD_CRON_SERVICE ~ '.name') }}" +NEXTCLOUD_TASKPROCESSING_SERVICE: "taskprocessing" +NEXTCLOUD_TASKPROCESSING_CONTAINER: "{{ lookup('config', application_id, 'services.' ~ NEXTCLOUD_TASKPROCESSING_SERVICE ~ '.name') }}" + NEXTCLOUD_HPB_SERVICE: "talk" NEXTCLOUD_HPB_CONTAINER: "{{ lookup('config', application_id, 'services.' ~ NEXTCLOUD_HPB_SERVICE ~ '.name') }}" NEXTCLOUD_HPB_NETWORK_MODE: "{{ lookup('config', application_id, 'services.' ~ NEXTCLOUD_HPB_SERVICE ~ '.network_mode') }}" diff --git a/tests/unit/python/roles/web-app-nextcloud/test_talk_direct_media.py b/tests/unit/python/roles/web-app-nextcloud/test_talk_direct_media.py index 9d3ddb3211..40eaa4c4c3 100644 --- a/tests/unit/python/roles/web-app-nextcloud/test_talk_direct_media.py +++ b/tests/unit/python/roles/web-app-nextcloud/test_talk_direct_media.py @@ -114,6 +114,7 @@ def _render( "NEXTCLOUD_PROXY_SERVICE": "proxy", "NEXTCLOUD_SERVICE": "nextcloud", "NEXTCLOUD_CRON_SERVICE": "cron", + "NEXTCLOUD_TASKPROCESSING_SERVICE": "taskprocessing", "DEPLOYMENT_MODE": mode, "NEXTCLOUD_HPB_SIGNALING_ENABLED": True, "NEXTCLOUD_HPB_SERVICE": "talk", diff --git a/tests/unit/python/roles/web-app-nextcloud/test_taskprocessing_worker.py b/tests/unit/python/roles/web-app-nextcloud/test_taskprocessing_worker.py new file mode 100644 index 0000000000..a17656b30e --- /dev/null +++ b/tests/unit/python/roles/web-app-nextcloud/test_taskprocessing_worker.py @@ -0,0 +1,77 @@ +from __future__ import annotations + +import re +import unittest + +from jinja2 import ChainableUndefined, Environment, select_autoescape + +from plugins.lookup.container_volumes import container_volumes +from utils.cache.files import PROJECT_ROOT, read_text +from utils.cache.yaml import load_yaml_any +from utils.roles.mapping import ( + ROLE_FILE_META_SERVICES, + ROLE_FILE_TEMPL_COMPOSE, + ROLE_FILE_VARS_MAIN, +) + +ROLE = "roles/web-app-nextcloud" +APPLICATION_ID = "web-app-nextcloud" +SERVICES = load_yaml_any(str(PROJECT_ROOT / ROLE / ROLE_FILE_META_SERVICES)) +VARS = load_yaml_any(str(PROJECT_ROOT / ROLE / ROLE_FILE_VARS_MAIN)) +TEMPLATE = read_text(str(PROJECT_ROOT / ROLE / ROLE_FILE_TEMPL_COMPOSE)) + + +def _render_jinja(expression: str) -> str: + env = Environment( + undefined=ChainableUndefined, + autoescape=select_autoescape(default_for_string=False), + ) + env.globals["lookup"] = lambda name, *args, **kwargs: f"<{name} {args!r}>" + env.filters["path_join"] = lambda parts: "/".join(str(part) for part in parts) + return env.from_string(expression).render() + + +def _mounts(service: str) -> str: + return container_volumes( + {APPLICATION_ID: {"services": SERVICES}}, + APPLICATION_ID, + service, + render_jinja=_render_jinja, + ) + + +def _service_block(service_var: str) -> str: + start = TEMPLATE.index(f"{{% set service_name = {service_var} %}}") + following = re.search(r"\{% (set service_name|if|endif)\b", TEMPLATE[start + 1 :]) + end = start + 1 + following.start() if following else len(TEMPLATE) + return TEMPLATE[start:end] + + +class TestTaskProcessingWorker(unittest.TestCase): + def test_service_key_matches_the_vars_spot(self): + self.assertIn(VARS["NEXTCLOUD_TASKPROCESSING_SERVICE"], SERVICES) + + def test_worker_mounts_exactly_what_cron_mounts(self): + cron = _mounts(VARS["NEXTCLOUD_CRON_SERVICE"]) + self.assertIn("/var/www/html/", cron) + self.assertEqual(_mounts(VARS["NEXTCLOUD_TASKPROCESSING_SERVICE"]), cron) + + def test_worker_scales_with_the_swarm_topology(self): + self.assertNotIn("replicas", SERVICES[VARS["NEXTCLOUD_TASKPROCESSING_SERVICE"]]) + + def test_worker_runs_the_command_that_records_its_heartbeat(self): + block = _service_block("NEXTCLOUD_TASKPROCESSING_SERVICE") + self.assertIn("taskprocessing:worker -t 60", block) + self.assertNotIn("background-job:worker", block) + + def test_worker_runs_the_cron_image_as_the_occ_owner(self): + worker = _service_block("NEXTCLOUD_TASKPROCESSING_SERVICE") + cron = _service_block("NEXTCLOUD_CRON_SERVICE") + image = "{{ lookup('container_image', application_id, 'nextcloud') }}" + self.assertIn(image, cron) + self.assertIn(image, worker) + self.assertIn('"su", "{{ NEXTCLOUD_DOCKER_USER }}"', worker) + + +if __name__ == "__main__": + unittest.main()