Skip to content

Commit

Permalink
Move dev db wait to db wait
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed Oct 11, 2023
1 parent e02e8eb commit 4096c2c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 38 deletions.
25 changes: 24 additions & 1 deletion bolt-db/bolt/db/cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import time
import subprocess
import sys

import click

from bolt.db import DEFAULT_DB_ALIAS, connections
from bolt.db import DEFAULT_DB_ALIAS, connections, OperationalError


@click.group()
Expand Down Expand Up @@ -49,3 +50,25 @@ def shell(database, parameters):
err=True,
)
sys.exit(e.returncode)


@cli.command()
def wait():
"""Wait for the database to be ready"""
attempts = 0
while True:
attempts += 1
waiting_for = []

for conn in connections.all():
try:
conn.ensure_connection()
except OperationalError:
waiting_for.append(conn.alias)

if waiting_for:
click.secho(f"Waiting for database (attempt {attempts}): {', '.join(waiting_for)}", fg="yellow")
time.sleep(1.5)
else:
click.secho(f"Database ready: {', '.join(connections)}", fg="green")
break
5 changes: 0 additions & 5 deletions bolt-dev/bolt/dev/db/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ def shell():
DBContainer().shell()


@cli.command()
def wait():
DBContainer().wait()


@cli.command()
def reset():
DBContainer().reset(create=True)
Expand Down
31 changes: 0 additions & 31 deletions bolt-dev/bolt/dev/db/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,37 +78,6 @@ def stop(self):
print(e.stderr.decode())
raise

def wait(self):
print("Waiting for database...")
print(f" Name: {self.name}")
print(f" Port: {self.postgres_port}")
print(f" DB: {self.postgres_db}")
print(f" User: {self.postgres_user}")

attempts = 1

while True:
if self.is_connected():
print("Database connected")
break
else:
print(f"Database unavailable, waiting 1 second... (attempt {attempts})")
time.sleep(1)
attempts += 1

def is_connected(self):
result = subprocess.run(
[
"bolt",
"legacy",
"showmigrations",
"--skip-checks",
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
return result.returncode == 0

def logs(self):
subprocess.check_call(
[
Expand Down
2 changes: 1 addition & 1 deletion bolt-dev/bolt/dev/work/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def cli():
gunicorn = f"gunicorn --reload bolt.wsgi:app --timeout 0 --workers 2 --access-logfile - --error-logfile - {reload_extra} --access-logformat '\"%(r)s\" status=%(s)s length=%(b)s dur=%(M)sms'"

if bolt_db_installed:
runserver_cmd = f"bolt dev db wait && bolt legacy migrate && {gunicorn}"
runserver_cmd = f"bolt db wait && bolt legacy migrate && {gunicorn}"
manager.add_process("postgres", "bolt dev db start --logs")
else:
runserver_cmd = gunicorn
Expand Down

0 comments on commit 4096c2c

Please sign in to comment.