Add "wait_for_db" management command

This commit is contained in:
Oliver Walters 2021-04-02 00:03:56 +11:00
parent 8e7e36089b
commit be41be3981
3 changed files with 32 additions and 5 deletions

View File

@ -0,0 +1,31 @@
"""
Custom management command, wait for the database to be ready!
"""
from django.core.management.base import BaseCommand
from django.db import connections
from django.db.utils import OperationalError
import time
class Command(BaseCommand):
"""
django command to pause execution until the database is ready
"""
def handle(self, *args, **kwargs):
self.stdout.write("Waiting for database...")
db_conn = None
while not db_conn:
try:
# get the database with keyword 'default' from settings.py
db_conn = connections['default']
# prints success messge in green
self.stdout.write(self.style.SUCCESS('InvenTree database connected'))
except OperationalError:
self.stdout.write("Database unavailable, waiting 5 seconds ...")
time.sleep(5)

View File

@ -10,11 +10,6 @@ database:
# Uncomment (and edit) one of the database configurations below,
# or specify database options using environment variables
# Default installation uses a simple sqlite database
# For production, consider changing this!
ENGINE: sqlite3
NAME: '/home/inventree/database.sqlite3'
# Refer to the django documentation for full list of options
# --- Available options: ---

View File

@ -23,6 +23,7 @@ ENV INVENTREE_PORT="${INVENTREE_PORT}"
# InvenTree paths
ENV INVENTREE_SRC_DIR="${INVENTREE_HOME}/src"
ENV INVENTREE_MNG_DIR="${INVENTREE_SRC_DIR}/InvenTree"
ENV INVENTREE_STATIC_ROOT="${INVENTREE_HOME}/static"
ENV INVENTREE_MEDIA_ROOT="${INVENTREE_HOME}/media"
ENV INVENTREE_LOG_DIR="${INVENTREE_HOME}/log"