Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle service health checks with Django #574

Merged
merged 2 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,12 @@ jobs:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
redis:
image: redis:7.4.0-alpine3.20
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- uses: actions/checkout@v4.1.7
Expand Down Expand Up @@ -84,4 +74,7 @@ jobs:
CELERY_RESULT_BACKEND: ""
DATABASE_URL: postgres://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres
SECRET_KEY: ""
run: poetry run python manage.py test --parallel --shuffle
run: |
poetry run python manage.py wait_for_db
poetry run python manage.py wait_for_cache
poetry run python manage.py test --parallel --shuffle
32 changes: 32 additions & 0 deletions src/core/management/commands/wait_for_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Command to wait for the cache to be available before proceeding.

Inspired from https://github.com/enzofrnt/django_wait_for_db
"""

import time

import djclick as click
from django.core.cache import caches
from redis.exceptions import ConnectionError as RedisConnectionError


@click.command()
def wait_for_cache():
click.secho("Waiting for the cache to be available...", fg="yellow")

while True:
try:
cache = caches["default"]
client = cache._cache.get_client()
client.ping()

click.secho("Cache available!", fg="green")

break

except RedisConnectionError as e:
click.secho(f"Error with cache: {e}", fg="red")
click.secho("Cache unavailable, waiting 1 second...", fg="yellow")

time.sleep(1)
31 changes: 31 additions & 0 deletions src/core/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Command to wait for the database to be available before proceeding.

Inspired from https://github.com/enzofrnt/django_wait_for_db
"""

import time

import djclick as click
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.utils import OperationalError


@click.command()
def wait_for_db():
click.secho("Waiting for the database to be available...", fg="yellow")

while True:
try:
connection = connections[DEFAULT_DB_ALIAS]
connection.ensure_connection()

click.secho("Database available!", fg="green")

break

except OperationalError as e:
click.secho(f"Error with database: {e}", fg="red")
click.secho("Database unavailable, waiting 1 second...", fg="yellow")

time.sleep(1)