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

Ping DB instead of trying migration #1513

Merged
merged 5 commits into from
Feb 11, 2022
Merged
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM python:3.9-alpine3.12

#Install all dependencies.
RUN apk add --no-cache postgresql-libs gettext zlib libjpeg libwebp libxml2-dev libxslt-dev py-cryptography
RUN apk add --no-cache postgresql-libs postgresql-client gettext zlib libjpeg libwebp libxml2-dev libxslt-dev py-cryptography

#Print all logs without buffering it.
ENV PYTHONUNBUFFERED 1
Expand Down
24 changes: 12 additions & 12 deletions boot.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#!/bin/sh
source venv/bin/activate

echo "Migrating database"
echo "Waiting for database to be ready..."

attempt=0
max_attempts=20
while python manage.py migrate; \
status=$?; \
attempt=$((attempt+1)); \
[ $status -eq 1 ] \
&& [ $attempt -le $max_attempts ]; do
echo -e "\n!!! Migration failed (error ${status}, attempt ${attempt}/${max_attempts})."
echo "!!! Database may not be ready yet or system is misconfigured."
echo -e "!!! Retrying in 5 seconds...\n"
while pg_isready --host=${POSTGRES_HOST} -q; status=$?; attempt=$((attempt+1)); [ $status -ne 0 ] && [ $attempt -le $max_attempts ]; do
sleep 5
done

if [ $attempt -gt $max_attempts ]; then
echo -e "\n!!! Migration failed. Maximum attempts exceeded."
echo "!!! Please check logs above - misconfiguration is very likely."
echo "!!! Shutting down container."
echo -e "\nDatabase not reachable. Maximum attempts exceeded."
echo "Please check logs above - misconfiguration is very likely."
echo "Make sure the DB container is up and POSTGRES_HOST is set properly."
echo "Shutting down container."
exit 1 # exit with error to make the container stop
fi

echo "Database is ready"

echo "Migrating database"

python manage.py migrate

echo "Generating static files"

python manage.py collectstatic_js_reverse
Expand Down