-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle database health check with Django
- Loading branch information
Showing
2 changed files
with
32 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
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: | ||
db_conn = connections[DEFAULT_DB_ALIAS] | ||
db_conn.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) |