From 9b709e86c9864f8b3a36adb33b5a168f79a38b64 Mon Sep 17 00:00:00 2001 From: Vadim Stepanov Date: Thu, 2 Feb 2023 09:08:48 +0000 Subject: [PATCH] Fix local dev setup slowness (#1270) # What this PR does Fixes an issue when a local dev setup becomes extremely slow. - Set `DEBUG` and `SILK_PROFILER_ENABLED` to `False` by default + add utility make commands to toggle it - Use `uwsgi` instead of Django's built-in `runserver` for local dev setup - Limit Celery concurrency to 3 for local dev setup (previously was 20, used >1GB RAM on my machine) --------- Co-authored-by: Joey Orlando --- Makefile | 11 +++++++++++ dev/README.md | 8 ++++++-- docker-compose-developer.yml | 3 +-- engine/engine/management/commands/start_celery.py | 2 +- engine/settings/dev.py | 3 ++- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 711e3e3d..809f70c6 100644 --- a/Makefile +++ b/Makefile @@ -126,6 +126,17 @@ engine-manage: exec-engine: docker exec -it oncall_engine bash +_backend-debug-enable: + $(shell ./dev/add_env_var.sh DEBUG True $(DEV_ENV_FILE)) + $(shell ./dev/add_env_var.sh SILK_PROFILER_ENABLED True $(DEV_ENV_FILE)) + +_backend-debug-disable: + $(shell ./dev/add_env_var.sh DEBUG False $(DEV_ENV_FILE)) + $(shell ./dev/add_env_var.sh SILK_PROFILER_ENABLED False $(DEV_ENV_FILE)) + +backend-debug-enable: _backend-debug-enable stop start +backend-debug-disable: _backend-debug-disable stop start + _enable-mobile-app-feature-flags: $(shell ./dev/add_env_var.sh FEATURE_MOBILE_APP_INTEGRATION_ENABLED True $(DEV_ENV_FILE)) $(call run_engine_docker_command,python manage.py enable_mobile_app) diff --git a/dev/README.md b/dev/README.md index f0dbd4db..b1b2ee65 100644 --- a/dev/README.md +++ b/dev/README.md @@ -105,8 +105,9 @@ volume mounted inside the container. In order to setup [`django-silk`](https://github.com/jazzband/django-silk) for local profiling, perform the following steps: -1. `make engine-manage CMD="createsuperuser"` - follow CLI prompts to create a Django superuser -2. Visit and login using the credentials you created in step #2 +1. `make backend-debug-enable` +2. `make engine-manage CMD="createsuperuser"` - follow CLI prompts to create a Django superuser +3. Visit and login using the credentials you created in step #2 You should now be able to visit and see the Django Silk UI. See the `django-silk` documentation [here](https://github.com/jazzband/django-silk) for more information. @@ -154,6 +155,9 @@ make engine-manage CMD="..." # NOTE: you need to enable, and configure, the plugin before running this command make enable-mobile-app-feature-flags +make backend-debug-enable # enable Django's debug mode and Silk profiling (this is disabled by default for performance reasons) +make backend-debug-disable # disable Django's debug mode and Silk profiling + # this will remove all of the images, containers, volumes, and networks # associated with your local OnCall developer setup make cleanup diff --git a/docker-compose-developer.yml b/docker-compose-developer.yml index c0212afc..133df6e6 100644 --- a/docker-compose-developer.yml +++ b/docker-compose-developer.yml @@ -26,7 +26,6 @@ x-env-vars: &oncall-env-vars GRAFANA_API_URL: http://localhost:3000 GOOGLE_APPLICATION_CREDENTIALS: /etc/app/gcp_service_account.json FCM_PROJECT_ID: oncall-mobile-dev - SILK_PROFILER_ENABLED: True # basically this is needed because the oncall backend containers have been configured to communicate w/ grafana via # http://localhost:3000 (GRAFANA_API_URL). This URL is used in two scenarios: @@ -64,7 +63,7 @@ services: labels: *oncall-labels build: *oncall-build-args restart: always - command: "python manage.py runserver 0.0.0.0:8080" + command: sh -c "uwsgi --disable-logging --py-autoreload 3 --ini uwsgi.ini" env_file: *oncall-env-files environment: *oncall-env-vars volumes: *oncall-volumes diff --git a/engine/engine/management/commands/start_celery.py b/engine/engine/management/commands/start_celery.py index 5c935036..e61a9430 100644 --- a/engine/engine/management/commands/start_celery.py +++ b/engine/engine/management/commands/start_celery.py @@ -11,7 +11,7 @@ def restart_celery(*args, **kwargs): global WORKER_ID kill_worker_cmd = "celery -A engine control shutdown" subprocess.call(shlex.split(kill_worker_cmd)) - start_worker_cmd = "celery -A engine worker -l info --concurrency=20 -Q celery,retry -n {}".format(WORKER_ID) + start_worker_cmd = "celery -A engine worker -l info --concurrency=3 -Q celery,retry -n {}".format(WORKER_ID) subprocess.call(shlex.split(start_worker_cmd)) WORKER_ID = 1 + WORKER_ID diff --git a/engine/settings/dev.py b/engine/settings/dev.py index e4843192..63c503a8 100644 --- a/engine/settings/dev.py +++ b/engine/settings/dev.py @@ -5,7 +5,8 @@ import sys from .base import * -DEBUG = True +# DEBUG is disabled by default because it can cause slowness when making several consecutive requests +DEBUG = getenv_boolean("DEBUG", default=False) if DATABASE_TYPE == DatabaseTypes.SQLITE3: DATABASES["default"]["NAME"] = DATABASE_NAME or "oncall_dev.db"