Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script wait_db.py to Django #2

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,9 @@
# Exporting all environment variables to use in crontab
env | sed 's/^\(.*\)$/ \1/g' > /root/env

function_postgres_ready() {
python << END
import socket
import time
import os

port = int(os.environ["POSTGRES_PORT"])

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect(('slave-db', port))
s.close()
END
}

echo '======= CHECKING FOR UNINSTALLED PKGs AND INSTALLING'
pip freeze || pip install -r requirements.txt

until function_postgres_ready; do
>&2 echo "======= POSTGRES IS UNAVAILABLE, WAITING"
sleep 1
done
echo "======= POSTGRES IS UP, CONNECTING"

echo '======= MAKING MIGRATIONS'
python3 manage.py makemigrations

Expand Down
3 changes: 3 additions & 0 deletions smi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import os
import environ
from .wait_db import start_services

env = environ.Env()

Expand Down Expand Up @@ -103,6 +104,8 @@
}
}

start_services()

# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
Expand Down
56 changes: 56 additions & 0 deletions smi/wait_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import importlib
import os
import time

import logging

SERVICES_STARTED = False

def start_services():
global SERVICES_STARTED

if SERVICES_STARTED:
return

start_postgres()

SERVICES_STARTED = True


log = logging.getLogger()


def start_postgres():
settings_path = os.environ['DJANGO_SETTINGS_MODULE']
settings = importlib.import_module(settings_path)

db = settings.DATABASES['default']
dbname = db['NAME']
user = db['USER']
password = db['PASSWORD']
host = db['HOST']

for _ in range(100):
if can_connect(dbname, user, password, host):
log.info("======= POSTGRES IS UP, CONNECTING")
return
log.warning('======= POSTGRES IS UNAVAILABLE, WAITING 0.5 Seconds')
time.sleep(0.5)

log.critical('Maximum number of attempts connecting to postgres database')
raise RuntimeError('could not connect to database')


def can_connect(dbname, user, password, host):
import psycopg2

try:
psycopg2.connect(
dbname=dbname,
user=user,
password=password,
host=host
)
except psycopg2.OperationalError:
return False
return True