Skip to content

Commit

Permalink
Refactored wait_for_db command to be more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
mghaznav committed Jul 11, 2024
1 parent 5c1248a commit 85bc491
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 8 additions & 9 deletions app/core/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import time

from psycopg2 import OperationalError as Psycopg2Error

from django.db import connections
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand


class Command(BaseCommand):

def handle(self, *args, **options):
self.stdout.write('waiting for database...')
db_up = False
self.stdout.write('Waiting for database...')
db_conn = None

while db_up is False:
while not db_conn:
try:
self.check(databases=['default'])
db_up = True
except (Psycopg2Error, OperationalError):
db_conn = connections['default']
db_conn.ensure_connection()
except OperationalError:
self.stdout.write('Database unavailable, waiting 1 second...')
time.sleep(1)

self.stdout.write(self.style.SUCCESS('Database available!'))

2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.9"

services:
app:
build:
Expand Down

0 comments on commit 85bc491

Please sign in to comment.