Files
serienbrief_django/app/mailmerge/management/commands/wait_for_db.py
T

26 lines
876 B
Python
Raw Normal View History

2026-05-21 10:36:16 +02:00
"""Wartet, bis die DB Connections annimmt."""
import time
from django.core.management.base import BaseCommand
from django.db import OperationalError, connections
class Command(BaseCommand):
help = "Wartet, bis die Standard-DB verfügbar ist."
def add_arguments(self, parser):
parser.add_argument("--timeout", type=int, default=60)
def handle(self, *args, **opts):
deadline = time.time() + opts["timeout"]
while time.time() < deadline:
try:
connections["default"].ensure_connection()
self.stdout.write(self.style.SUCCESS("DB ist bereit."))
return
except OperationalError:
self.stdout.write("warte auf DB...")
time.sleep(2)
self.stderr.write(self.style.ERROR("DB nach Timeout nicht erreichbar."))
raise SystemExit(1)