From dd12a593f4ffd6e1999e03ce4de807fbf0430595 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 29 Jul 2021 16:37:34 +1000 Subject: [PATCH 1/2] Specify how many workers to use --- InvenTree/InvenTree/settings.py | 15 ++++++++++++++- docker/Dockerfile | 4 ++++ docker/gunicorn.conf.py | 18 +++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index f9ecaa394d..9e0fa128c9 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -12,6 +12,7 @@ database setup in this file. """ import logging +from multiprocessing import Value import os import random import string @@ -347,10 +348,22 @@ REST_FRAMEWORK = { WSGI_APPLICATION = 'InvenTree.wsgi.application' +background_workers = os.environ.get('INVENTREE_BACKGROUND_WORKERS', None) + +if background_workers is not None: + try: + background_workers = int(background_workers) + except ValueError: + background_workers = None + +if background_workers is None: + # Sensible default? + background_workers = 4 + # django-q configuration Q_CLUSTER = { 'name': 'InvenTree', - 'workers': 4, + 'workers': background_workers, 'timeout': 90, 'retry': 120, 'queue_limit': 50, diff --git a/docker/Dockerfile b/docker/Dockerfile index 72e6acc8ca..b6005ea49c 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -27,6 +27,10 @@ ENV INVENTREE_MEDIA_ROOT="${INVENTREE_DATA_DIR}/media" ENV INVENTREE_CONFIG_FILE="${INVENTREE_DATA_DIR}/config.yaml" ENV INVENTREE_SECRET_KEY_FILE="${INVENTREE_DATA_DIR}/secret_key.txt" +# Worker configuration (can be altered by user) +ENV INVENTREE_GUNICORN_WORKERS="4" +ENV INVENTREE_BACKGROUND_WORKERS="4" + # Default web server port is 8000 ENV INVENTREE_WEB_PORT="8000" diff --git a/docker/gunicorn.conf.py b/docker/gunicorn.conf.py index 1071c2d745..758d85b8ee 100644 --- a/docker/gunicorn.conf.py +++ b/docker/gunicorn.conf.py @@ -1,6 +1,22 @@ import multiprocessing +import os +import logging -workers = multiprocessing.cpu_count() * 2 + 1 + +logger = logging.get('inventree') + +workers = os.environ.get('INVENTREE_GUNICORN_WORKERS', None) + +if workers is not None: + try: + workers = int(workers) + except ValueError: + workers = None + +if workers is None: + workers = multiprocessing.cpu_count() * 2 + 1 + +logger.info(f"Starting gunicorn server with {workers} workers") max_requests = 1000 max_requests_jitter = 50 From 542c204ca074db0a97d12a72db044a59243ffbd1 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 29 Jul 2021 16:39:51 +1000 Subject: [PATCH 2/2] PEP fixes --- InvenTree/InvenTree/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 9e0fa128c9..4543b873bd 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -12,7 +12,7 @@ database setup in this file. """ import logging -from multiprocessing import Value + import os import random import string