From ed13f002b12a5c8c65d08ab81f510efaffcbc560 Mon Sep 17 00:00:00 2001 From: Sergio Fenoll Date: Thu, 16 Nov 2023 22:56:15 +0100 Subject: [PATCH 1/6] feat: use gunicorn in dev mode --- start.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/start.sh b/start.sh index 46e4931..bf12228 100644 --- a/start.sh +++ b/start.sh @@ -2,7 +2,7 @@ set -eu if [ ${MODE:-""} == "development" ]; then if [ -f /app/requirements.txt ]; then pip install -r /app/requirements.txt; fi - exec python web.py + exec gunicorn -k egg:meinheld#gunicorn_worker -c "$GUNICORN_CONF" "$APP_MODULE" else exec gunicorn -k egg:meinheld#gunicorn_worker -c "$GUNICORN_CONF" "$APP_MODULE" fi From e4c28bee74367c8b0b38c3e0ac78e18424128d6d Mon Sep 17 00:00:00 2001 From: Sergio Fenoll Date: Thu, 16 Nov 2023 22:56:29 +0100 Subject: [PATCH 2/6] chore: enable live reload in dev mode with gunicorn --- start.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/start.sh b/start.sh index bf12228..4ddeab2 100644 --- a/start.sh +++ b/start.sh @@ -2,7 +2,7 @@ set -eu if [ ${MODE:-""} == "development" ]; then if [ -f /app/requirements.txt ]; then pip install -r /app/requirements.txt; fi - exec gunicorn -k egg:meinheld#gunicorn_worker -c "$GUNICORN_CONF" "$APP_MODULE" + exec gunicorn -k --reload egg:meinheld#gunicorn_worker -c "$GUNICORN_CONF" "$APP_MODULE" else exec gunicorn -k egg:meinheld#gunicorn_worker -c "$GUNICORN_CONF" "$APP_MODULE" fi From 5981fc64b86047b246b9f544ffe8763ac258b62d Mon Sep 17 00:00:00 2001 From: Sergio Fenoll Date: Thu, 16 Nov 2023 22:57:25 +0100 Subject: [PATCH 3/6] feat: replace gunicorn worker with gthread The meinheld worker seems to be unmaintained and it doesn't work with gunicorn's reload mechanism. --- start.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/start.sh b/start.sh index 4ddeab2..9c2ca74 100644 --- a/start.sh +++ b/start.sh @@ -2,7 +2,7 @@ set -eu if [ ${MODE:-""} == "development" ]; then if [ -f /app/requirements.txt ]; then pip install -r /app/requirements.txt; fi - exec gunicorn -k --reload egg:meinheld#gunicorn_worker -c "$GUNICORN_CONF" "$APP_MODULE" + exec gunicorn -k gthread --reload -c "$GUNICORN_CONF" "$APP_MODULE" else - exec gunicorn -k egg:meinheld#gunicorn_worker -c "$GUNICORN_CONF" "$APP_MODULE" + exec gunicorn -k gthread -c "$GUNICORN_CONF" "$APP_MODULE" fi From 8f2a1c483faf206278c6ba7ff0c5a63506dfb862 Mon Sep 17 00:00:00 2001 From: Sergio Fenoll Date: Thu, 16 Nov 2023 23:34:09 +0100 Subject: [PATCH 4/6] feat: add inotify to requirements Having inotify installed enables gunicorn's reload mechanism to survive through syntax errors and such in the service code. --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 3ae439a..0d51479 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ flask SPARQLWrapper rdflib +inotify From c39c54f5182b367b250630a5a00ef990e8383cee Mon Sep 17 00:00:00 2001 From: Sergio Fenoll Date: Fri, 17 Nov 2023 09:35:33 +0100 Subject: [PATCH 5/6] feat: don't wrap service code import in a try-except Using gunicorn's reload mechanism, importing the code in a try-except meant that the reloader would not pick up changes if the initial code import threw an error. The reloader checks which files it needs to listen to notifications for based on its imports. It seems that the combination of a throwing import inside a try block makes it so the reloader doesn't register the service entrypoint as a file it needs to listen to. --- web.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/web.py b/web.py index 813c9b2..faa9024 100644 --- a/web.py +++ b/web.py @@ -26,11 +26,8 @@ # Import the app from the service consuming the template app_file = os.environ.get('APP_ENTRYPOINT') -try: - module_path = 'ext.app.{}'.format(app_file) - import_module(module_path) -except Exception as e: - helpers.log(str(e)) +module_path = 'ext.app.{}'.format(app_file) +import_module(module_path) ####################### ## Start Application ## From 0101e0dcfe29a3cd62ecce806225b7754b318ca8 Mon Sep 17 00:00:00 2001 From: Sergio Fenoll Date: Fri, 17 Nov 2023 09:38:33 +0100 Subject: [PATCH 6/6] chore: remove Flask app run We no longer use Flask in dev mode, so this if block is never entered. --- web.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/web.py b/web.py index faa9024..62c8bac 100644 --- a/web.py +++ b/web.py @@ -28,10 +28,3 @@ app_file = os.environ.get('APP_ENTRYPOINT') module_path = 'ext.app.{}'.format(app_file) import_module(module_path) - -####################### -## Start Application ## -####################### -if __name__ == '__main__': - debug = os.environ.get('MODE') == "development" - app.run(debug=debug, host='0.0.0.0', port=80)