From 0fc643f5488b936aa220f980c3942012947afaad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Wed, 2 Aug 2023 10:50:29 +0200 Subject: [PATCH] fix: high memory/cpu usage in dev In development, the tutor-mfe plugin runs a separate container for every MFE. This runs `npm run start` for each of the 9 MFEs, which causes the developer laptops to crash and ice caps to melt. Here, we adopt a different solution. By default, all MFEs run in a single container, which is the same as the production container (with caddy). Only the Caddyfile is different. This effectively resolves the performance issue. But to allow developers to hack on invidual MFEs, we also need to run some of them in development mode, with `npm run start`. To do that, we run separately only those MFEs for which the developer has a bind-mounted directory that corresponds to the MFE repository. This allows developers to keep working on MFEs with the help of `tutor mounts ...` command. --- ...30802_121750_regis_single_dev_container.md | 1 + tutormfe/patches/caddyfile | 5 ----- .../patches/local-docker-compose-dev-services | 20 ++++++++++++++----- ...services => local-docker-compose-services} | 0 tutormfe/templates/mfe/apps/mfe/Caddyfile | 6 ++++++ 5 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 changelog.d/20230802_121750_regis_single_dev_container.md rename tutormfe/patches/{local-docker-compose-prod-services => local-docker-compose-services} (100%) diff --git a/changelog.d/20230802_121750_regis_single_dev_container.md b/changelog.d/20230802_121750_regis_single_dev_container.md new file mode 100644 index 0000000..82ba56e --- /dev/null +++ b/changelog.d/20230802_121750_regis_single_dev_container.md @@ -0,0 +1 @@ +- [Improvement] Fix very high CPU and memory usage in development. We resolve this issue by running just a single container for all MFES, just like in production. To al;low developers to test their changes in Tutor, we run `npm run start` only for those MFEs that have a manual bind-mount that was created with `tutor mounts add .../frontend-app-mymfe`. (by @regisb) diff --git a/tutormfe/patches/caddyfile b/tutormfe/patches/caddyfile index edab101..55dec86 100644 --- a/tutormfe/patches/caddyfile +++ b/tutormfe/patches/caddyfile @@ -3,10 +3,5 @@ request_body { max_size 2MB } - reverse_proxy /api/mfe_config/v1* lms:8000 { - # We need to specify the host header, otherwise it will be rejected with 400 - # from the lms. - header_up Host {{ LMS_HOST }} - } import proxy "mfe:8002" } diff --git a/tutormfe/patches/local-docker-compose-dev-services b/tutormfe/patches/local-docker-compose-dev-services index 293f936..39319c1 100644 --- a/tutormfe/patches/local-docker-compose-dev-services +++ b/tutormfe/patches/local-docker-compose-dev-services @@ -1,6 +1,15 @@ -# MFE apps -{% for app_name, app in iter_mfes() %} -{{ app_name }}: +mfe: + ports: + {%- for app_name, app in iter_mfes() %} + {%- if not iter_mounts(MOUNTS, app_name)|list %} + - {{ app["port"] }}:8002 # {{ app_name }} + {%- endif %} + {%- endfor %} + +{%- for app_name, app in iter_mfes() %} +{%- set mounts = iter_mounts(MOUNTS, app_name)|list %} +{%- if mounts %} +{{ app_name }}: # Work on this MFE for development image: "{{ DOCKER_REGISTRY }}overhangio/openedx-{{ app_name }}-dev:{{ MFE_VERSION }}" ports: - "{{ app["port"] }}:{{ app["port"] }}" @@ -8,10 +17,11 @@ tty: true volumes: - ../plugins/mfe/apps/mfe/webpack.dev-tutor.config.js:/openedx/app/webpack.dev-tutor.config.js:ro - {%- for mount in iter_mounts(MOUNTS, app_name) %} + {%- for mount in mounts %} - {{ mount }} {%- endfor %} restart: unless-stopped depends_on: - lms -{% endfor %} +{%- endif %} +{%- endfor %} diff --git a/tutormfe/patches/local-docker-compose-prod-services b/tutormfe/patches/local-docker-compose-services similarity index 100% rename from tutormfe/patches/local-docker-compose-prod-services rename to tutormfe/patches/local-docker-compose-services diff --git a/tutormfe/templates/mfe/apps/mfe/Caddyfile b/tutormfe/templates/mfe/apps/mfe/Caddyfile index 78e42df..ec6a9b4 100644 --- a/tutormfe/templates/mfe/apps/mfe/Caddyfile +++ b/tutormfe/templates/mfe/apps/mfe/Caddyfile @@ -12,6 +12,12 @@ } } + reverse_proxy /api/mfe_config/v1* lms:8000 { + # We need to specify the host header, otherwise it will be rejected with 400 + # from the lms. + header_up Host {{ LMS_HOST }} + } + {% for app_name, app in iter_mfes() %} @mfe_{{ app_name }} { path /{{ app_name }} /{{ app_name }}/*