Skip to content

Commit

Permalink
add health check for containers
Browse files Browse the repository at this point in the history
  • Loading branch information
sainak committed Dec 28, 2023
1 parent 0cbf284 commit 2be3bcd
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ hashFiles('Pipfile.lock', 'docker/prod.Dockerfile') }}
key: ${{ runner.os }}-buildx-${{ hashFiles('Pipfile.lock', 'docker/dev.Dockerfile') }}
restore-keys: |
${{ runner.os }}-buildx-
Expand All @@ -30,7 +30,7 @@ jobs:
files: docker-compose.yaml,docker-compose.local.yaml

- name: Start services
run: docker compose -f docker-compose.yaml -f docker-compose.local.yaml up -d --no-build
run: docker compose -f docker-compose.yaml -f docker-compose.local.yaml up -d --wait --no-build

- name: Check migrations
run: make checkmigration
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ build:
docker compose -f docker-compose.yaml -f $(docker_config_file) build

up:
docker compose -f docker-compose.yaml -f $(docker_config_file) up -d
docker compose -f docker-compose.yaml -f $(docker_config_file) up -d --wait

down:
docker compose -f docker-compose.yaml -f $(docker_config_file) down
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ services:
depends_on:
- db
- redis
- celery

celery:
image: care_local
Expand All @@ -25,7 +26,6 @@ services:
entrypoint: [ "bash", "scripts/celery-dev.sh" ]
depends_on:
- db
- backend
- redis
volumes:
- .:/app
5 changes: 2 additions & 3 deletions docker-compose.pre-built.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
version: '3.4'

services:

backend:
image: "ghcr.io/coronasafe/care:latest"
env_file:
Expand All @@ -10,6 +9,7 @@ services:
depends_on:
- db
- redis
- celery-beat
ports:
- "9000:9000"

Expand All @@ -20,7 +20,6 @@ services:
entrypoint: [ "bash", "celery_worker-ecs.sh" ]
depends_on:
- db
- backend
- redis

celery-beat:
Expand All @@ -30,5 +29,5 @@ services:
entrypoint: [ "bash", "celery_beat-ecs.sh" ]
depends_on:
- db
- backend
- redis
- celery-beat
4 changes: 2 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ services:
- EDGE_PORT=4566
- SERVICES=s3
volumes:
- "${TEMPDIR:-/tmp/localstack}:/tmp/localstack"
- "./docker/awslocal:/docker-entrypoint-initaws.d"
- "${TEMPDIR:-./care/media/localstack}:/var/lib/localstack"
- "./docker/awslocal:/etc/localstack/init/ready.d/"

fidelius:
image: khavinshankar/fidelius:v1.0
Expand Down
7 changes: 7 additions & 0 deletions docker/dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ RUN pipenv install --system --categories "packages dev-packages"

COPY . /app

HEALTHCHECK \
--interval=5s \
--timeout=5s \
--start-period=5s \
--retries=24 \
CMD ["/app/scripts/healthcheck.sh"]

WORKDIR /app
9 changes: 8 additions & 1 deletion docker/prod.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ENV PATH /venv/bin:$PATH
WORKDIR ${APP_HOME}

RUN apt-get update && apt-get install --no-install-recommends -y \
libpq-dev gettext wget gnupg chromium \
libpq-dev gettext wget curl gnupg chromium \
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*

Expand All @@ -50,6 +50,13 @@ COPY --from=builder /venv /venv

COPY --chmod=0755 ./scripts/*.sh ./

HEALTHCHECK \
--interval=5s \
--timeout=5s \
--start-period=5s \
--retries=24 \
CMD ["/app/healthcheck.sh"]

COPY . ${APP_HOME}

EXPOSE 9000
34 changes: 34 additions & 0 deletions scripts/celery-dev.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
#!/bin/bash

printf "celery" >> /tmp/container-role

if [ -z "${DATABASE_URL}" ]; then
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
fi

postgres_ready() {
python << END
import sys
import psycopg
try:
psycopg.connect(conninfo="${DATABASE_URL}")
except psycopg.OperationalError as e:
print(e)
sys.exit(-1)
sys.exit(0)
END
}

until postgres_ready; do
>&2 echo 'Waiting for PostgreSQL to become available...'
sleep 1
done
>&2 echo 'PostgreSQL is available'

python manage.py migrate --noinput
python manage.py load_redis_index


touch /tmp/healthy

watchmedo \
auto-restart --directory=./ --pattern=*.py --recursive -- \
celery --workdir="/app" -A config.celery_app worker -B --loglevel=INFO
4 changes: 4 additions & 0 deletions scripts/celery_beat-ecs.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
printf "celery-beat" >> /tmp/container-role

if [ -z "${DATABASE_URL}" ]; then
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
Expand Down Expand Up @@ -29,4 +30,7 @@ done
python manage.py migrate --noinput
python manage.py load_redis_index


touch /tmp/healthy

celery --app=config.celery_app beat --loglevel=info
4 changes: 4 additions & 0 deletions scripts/celery_beat.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
printf "celery-beat" >> /tmp/container-role

if [ -z "${DATABASE_URL}" ]; then
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
Expand Down Expand Up @@ -29,5 +30,8 @@ done
python manage.py migrate --noinput
python manage.py load_redis_index


touch /tmp/healthy

export NEW_RELIC_CONFIG_FILE=/etc/newrelic.ini
newrelic-admin run-program celery --app=config.celery_app beat --loglevel=info
5 changes: 5 additions & 0 deletions scripts/celery_worker-ecs.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/bash
printf "celery-worker" >> /tmp/container-role

if [ -z "${DATABASE_URL}" ]; then
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
fi


touch /tmp/healthy

celery --app=config.celery_app worker --max-tasks-per-child=6 --loglevel=info
5 changes: 5 additions & 0 deletions scripts/celery_worker.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/bin/bash
printf "celery-worker" >> /tmp/container-role

if [ -z "${DATABASE_URL}" ]; then
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
fi


touch /tmp/healthy

export NEW_RELIC_CONFIG_FILE=/etc/newrelic.ini
newrelic-admin run-program celery --app=config.celery_app worker --max-tasks-per-child=6 --loglevel=info
13 changes: 13 additions & 0 deletions scripts/healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

CONTAINER_ROLE=$(cat /tmp/container-role)
if [[ "$CONTAINER_ROLE" = "api" ]]; then
curl -f http://localhost:9000/health/ || exit 1
else
# Health check for Worker/beat
if [[ -f /tmp/healthy ]]; then
exit 0
else
exit 1
fi
fi
5 changes: 2 additions & 3 deletions scripts/start-dev.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/usr/bin/env bash
set -euo pipefail

cd /app
printf "api" >> /tmp/container-role

echo "running migrations..."
python manage.py migrate
cd /app

echo "running collectstatic..."
python manage.py collectstatic --noinput
Expand Down
2 changes: 2 additions & 0 deletions scripts/start-ecs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set -o errexit
set -o pipefail
set -o nounset

printf "api" >> /tmp/container-role

if [ -z "${DATABASE_URL}" ]; then
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
fi
Expand Down
2 changes: 2 additions & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set -o errexit
set -o pipefail
set -o nounset

printf "api" >> /tmp/container-role

if [ -z "${DATABASE_URL}" ]; then
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"
fi
Expand Down

0 comments on commit 2be3bcd

Please sign in to comment.