From 773d2eff373999277ffbb937a6721804a3db91a9 Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Sun, 6 Feb 2022 18:54:42 +0100 Subject: [PATCH 1/7] Moved db-waiting from docker-level to container-level --- boot.sh | 5 ++++- docs/install/docker/nginx-proxy/docker-compose.yml | 8 +------- docs/install/docker/plain/docker-compose.yml | 8 +------- docs/install/docker/traefik-nginx/docker-compose.yml | 8 +------- 4 files changed, 7 insertions(+), 22 deletions(-) diff --git a/boot.sh b/boot.sh index 8cd2c22c..6683fe26 100644 --- a/boot.sh +++ b/boot.sh @@ -2,7 +2,10 @@ source venv/bin/activate echo "Updating database" -python manage.py migrate +while python manage.py migrate ; status=$? ; [ $status -eq 1 ]; do + echo "Migration failed due to database not being ready yet, retrying in 5 seconds..." + sleep 5 +done python manage.py collectstatic_js_reverse python manage.py collectstatic --noinput echo "Done" diff --git a/docs/install/docker/nginx-proxy/docker-compose.yml b/docs/install/docker/nginx-proxy/docker-compose.yml index 659b5bd5..077e57ab 100644 --- a/docs/install/docker/nginx-proxy/docker-compose.yml +++ b/docs/install/docker/nginx-proxy/docker-compose.yml @@ -9,11 +9,6 @@ services: - ./.env networks: - default - healthcheck: - test: ["CMD-SHELL", "psql -U $$POSTGRES_USER -d $$POSTGRES_DB --list || exit 1"] - interval: 4s - timeout: 1s - retries: 12 web_recipes: image: vabene1111/recipes @@ -25,8 +20,7 @@ services: - nginx_config:/opt/recipes/nginx/conf.d - ./mediafiles:/opt/recipes/mediafiles depends_on: - db_recipes: - condition: service_healthy + - db_recipes networks: - default diff --git a/docs/install/docker/plain/docker-compose.yml b/docs/install/docker/plain/docker-compose.yml index 1cd8fb1b..2e6f6e98 100644 --- a/docs/install/docker/plain/docker-compose.yml +++ b/docs/install/docker/plain/docker-compose.yml @@ -7,11 +7,6 @@ services: - ./postgresql:/var/lib/postgresql/data env_file: - ./.env - healthcheck: - test: ["CMD-SHELL", "psql -U $$POSTGRES_USER -d $$POSTGRES_DB --list || exit 1"] - interval: 4s - timeout: 1s - retries: 12 web_recipes: image: vabene1111/recipes @@ -23,8 +18,7 @@ services: - nginx_config:/opt/recipes/nginx/conf.d - ./mediafiles:/opt/recipes/mediafiles depends_on: - db_recipes: - condition: service_healthy + - db_recipes nginx_recipes: image: nginx:mainline-alpine diff --git a/docs/install/docker/traefik-nginx/docker-compose.yml b/docs/install/docker/traefik-nginx/docker-compose.yml index c68a25c9..05beaf98 100644 --- a/docs/install/docker/traefik-nginx/docker-compose.yml +++ b/docs/install/docker/traefik-nginx/docker-compose.yml @@ -9,11 +9,6 @@ services: - ./.env networks: - default - healthcheck: - test: ["CMD-SHELL", "psql -U $$POSTGRES_USER -d $$POSTGRES_DB --list || exit 1"] - interval: 4s - timeout: 1s - retries: 12 web_recipes: image: vabene1111/recipes @@ -25,8 +20,7 @@ services: - nginx_config:/opt/recipes/nginx/conf.d - ./mediafiles:/opt/recipes/mediafiles depends_on: - db_recipes: - condition: service_healthy + - db_recipes networks: - default From bb131ef16ab3205a7da1210e7ca586c501f45dd9 Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Sun, 6 Feb 2022 19:12:52 +0100 Subject: [PATCH 2/7] Improved wording of log message upon retry --- boot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/boot.sh b/boot.sh index 6683fe26..795f1214 100644 --- a/boot.sh +++ b/boot.sh @@ -3,7 +3,7 @@ source venv/bin/activate echo "Updating database" while python manage.py migrate ; status=$? ; [ $status -eq 1 ]; do - echo "Migration failed due to database not being ready yet, retrying in 5 seconds..." + echo "Migration failed! Database may not be ready yet, retrying in 5 seconds..." sleep 5 done python manage.py collectstatic_js_reverse @@ -12,4 +12,4 @@ echo "Done" chmod -R 755 /opt/recipes/mediafiles -exec gunicorn -b :8080 --access-logfile - --error-logfile - --log-level INFO recipes.wsgi \ No newline at end of file +exec gunicorn -b :8080 --access-logfile - --error-logfile - --log-level INFO recipes.wsgi From fd5de4e47c221a7bcb444db914c3dea1befa2bb7 Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Sun, 6 Feb 2022 19:22:46 +0100 Subject: [PATCH 3/7] Included error code in log message --- boot.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boot.sh b/boot.sh index 795f1214..af72a7b9 100644 --- a/boot.sh +++ b/boot.sh @@ -3,7 +3,7 @@ source venv/bin/activate echo "Updating database" while python manage.py migrate ; status=$? ; [ $status -eq 1 ]; do - echo "Migration failed! Database may not be ready yet, retrying in 5 seconds..." + echo "Migration failed (error #${status})! Database may not be ready yet, retrying in 5 seconds..." sleep 5 done python manage.py collectstatic_js_reverse From 20cc4b93a99b1628921247e1cd5fddaf5b50bd3f Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Mon, 7 Feb 2022 18:13:19 +0100 Subject: [PATCH 4/7] Add max amount of retries for web container boot --- boot.sh | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/boot.sh b/boot.sh index af72a7b9..7e2f05fa 100644 --- a/boot.sh +++ b/boot.sh @@ -1,13 +1,33 @@ #!/bin/sh source venv/bin/activate -echo "Updating database" -while python manage.py migrate ; status=$? ; [ $status -eq 1 ]; do - echo "Migration failed (error #${status})! Database may not be ready yet, retrying in 5 seconds..." - sleep 5 +echo "Migrating database" + +attempt=0 +max_attempts=20 +while python manage.py migrate; \ + status=$?; \ + attempt=$((attempt+1)); \ + [ $status -eq 1 ] \ + && [ $attempt -le $max_attempts ]; do + echo -e "\n!!! Migration failed (error ${status}, attempt ${attempt}/${max_attempts})." + echo "!!! Database may not be ready yet or system is misconfigured." + echo -e "!!! Retrying in 5 seconds...\n" + sleep 5 done + +if [ $attempt -gt $max_attempts ]; then + echo -e "\n!!! Migration failed. Maximum attempts exceeded." + echo "!!! Please check logs above - misconfiguration is very likely." + echo "!!! Shutting down container." + exit 1 # exit with error to make the container stop +fi + +echo "Generating static files" + python manage.py collectstatic_js_reverse python manage.py collectstatic --noinput + echo "Done" chmod -R 755 /opt/recipes/mediafiles From b53a9a1c079f7c94584fdf538a12279bb9b0d477 Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Mon, 7 Feb 2022 18:13:50 +0100 Subject: [PATCH 5/7] Remove always-restart so the container doesn't boot-loop on error --- docs/install/docker/nginx-proxy/docker-compose.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/install/docker/nginx-proxy/docker-compose.yml b/docs/install/docker/nginx-proxy/docker-compose.yml index 077e57ab..5c6ba87a 100644 --- a/docs/install/docker/nginx-proxy/docker-compose.yml +++ b/docs/install/docker/nginx-proxy/docker-compose.yml @@ -12,7 +12,6 @@ services: web_recipes: image: vabene1111/recipes - restart: always env_file: - ./.env volumes: From 855f1e4ee70852c184678da6bbe2a24f7e700360 Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Mon, 7 Feb 2022 18:13:58 +0100 Subject: [PATCH 6/7] Remove always-restart so the container doesn't boot-loop on error --- docs/install/docker/plain/docker-compose.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/install/docker/plain/docker-compose.yml b/docs/install/docker/plain/docker-compose.yml index 2e6f6e98..79ce307d 100644 --- a/docs/install/docker/plain/docker-compose.yml +++ b/docs/install/docker/plain/docker-compose.yml @@ -10,7 +10,6 @@ services: web_recipes: image: vabene1111/recipes - restart: always env_file: - ./.env volumes: From d28a2f81a2eac3749d487e76ffcf354c84b031fb Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Mon, 7 Feb 2022 18:14:03 +0100 Subject: [PATCH 7/7] Remove always-restart so the container doesn't boot-loop on error --- docs/install/docker/traefik-nginx/docker-compose.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/install/docker/traefik-nginx/docker-compose.yml b/docs/install/docker/traefik-nginx/docker-compose.yml index 05beaf98..fa5dae78 100644 --- a/docs/install/docker/traefik-nginx/docker-compose.yml +++ b/docs/install/docker/traefik-nginx/docker-compose.yml @@ -12,7 +12,6 @@ services: web_recipes: image: vabene1111/recipes - restart: always env_file: - ./.env volumes: