Skip to content

Commit c1d1359

Browse files
authored
Add a script that waits for the DB container to be available (#194)
## Ticket Resolves #195 ## Changes Added a shell script that uses the `pg_isready` command to check if the DB is available rather than arbitrarily waiting 5 seconds. Added some details for how to run with PY_RUN_APPROACH=local with some local.env changes that occurred a while back. ## Context for reviewers The notes regarding the PY_RUN_APPROACH and loading environment variables was needed to unblock my development, but only amounts to some updated documentation so should be fine to combine with this PR. ## Testing Was able to run this locally: ![Screenshot 2023-09-13 at 2 55 49 PM](https://github.com/navapbc/template-application-flask/assets/46358556/f7fbf65f-5ba0-4f2d-8a40-f1177934f48c)
1 parent 721421c commit c1d1359

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
# IDE-specific files
66
.vscode/*
7+
.idea

app/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ check: format-check lint test
114114
#########################
115115

116116
# Docker starts the image for the DB but it's not quite
117-
# fully ready, so add a 5 second sleep so upgrade doesn't
118-
# fail because the DB hasn't started yet.
119-
init-db: start-db sleep-5 db-migrate
117+
# ready to accept connections so we add a brief wait script
118+
init-db: start-db db-migrate
120119

121120
start-db:
122121
docker-compose up --detach main-db
122+
./bin/wait-for-local-db.sh
123123

124124
## Destroy current DB, setup new one
125125
db-recreate: clean-volumes init-db

app/bin/wait-for-local-db.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
# wait-for-local-db
3+
4+
set -e
5+
6+
# Color formatting
7+
RED='\033[0;31m'
8+
NO_COLOR='\033[0m'
9+
10+
MAX_WAIT_TIME=30 # seconds
11+
WAIT_TIME=0
12+
13+
# Use pg_isready to wait for the DB to be ready to accept connections
14+
# We check every 3 seconds and consider it failed if it gets to 30+
15+
# https://www.postgresql.org/docs/current/app-pg-isready.html
16+
until pg_isready -h localhost -d main-db -q;
17+
do
18+
echo "waiting on Postgres DB to initialize..."
19+
sleep 3
20+
21+
WAIT_TIME=$(($WAIT_TIME+3))
22+
if [ $WAIT_TIME -gt $MAX_WAIT_TIME ]
23+
then
24+
echo -e "${RED}ERROR: Database appears to not be starting up, running \"docker logs main-db\" to troubleshoot.${NO_COLOR}"
25+
docker logs main-db
26+
exit 1
27+
fi
28+
done
29+
30+
echo "Postgres DB is ready after ~${WAIT_TIME} seconds"
31+
32+

docs/app/README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,41 @@ Note that even with the native mode, many components like the DB and API will on
6464

6565
Running in the native/local approach may require additional packages to be installed on your machine to get working.
6666

67+
### Running Natively
68+
69+
* Run `export PY_RUN_APPROACH=local`
70+
* Run `make setup-local`
71+
* Run `poetry install --all-extras --with dev` to keep your Poetry packages up to date
72+
* Load environment variables from the local.env file, see below for one option.
73+
74+
One option for loading all of your local.env variables is to install direnv: https://direnv.net/
75+
You can configure direnv to then load the local.env file by creating an `.envrc` file in the /app directory that looks like:
76+
77+
```sh
78+
#!/bin/bash
79+
set -o allexport
80+
source local.env
81+
set +o allexport
82+
83+
# Set any environment variable overrides you want here
84+
#
85+
# If you are running outside of the Docker container, the DB can
86+
# be found on localhost:5432. Inside the container it's referenced via
87+
# the name of the docker container.
88+
export DB_HOST=localhost
89+
```
90+
And then running `direnv allow .` in the /app folder. You should see something like:
91+
```shell
92+
➜ template-application-flask git:(main) ✗ cd app
93+
direnv: loading ~/workspace/template-application-flask/app/.envrc
94+
direnv: export +API_AUTH_TOKEN +AWS_ACCESS_KEY_ID +AWS_DEFAULT_REGION +AWS_SECRET_ACCESS_KEY +DB_HOST +DB_NAME +DB_PASSWORD +DB_SCHEMA +DB_SSL_MODE +DB_USER +ENVIRONMENT +FLASK_APP +HIDE_SQL_PARAMETER_LOGS +LOG_ENABLE_AUDIT +LOG_FORMAT +PORT +PYTHONPATH
95+
```
96+
6797
## Environment Variables
6898

6999
Most configuration options are managed by environment variables.
70100

71-
Environment variables for local development are stored in the [local.env](/app/local.env) file. This file is automatically loaded when running. If running within Docker, this file is specified as an `env_file` in the [docker-compose](/docker-compose.yml) file, and loaded [by a script](/app/src/util/local.py) automatically when running most other components outside the container.
101+
Environment variables for local development are stored in the [local.env](/app/local.env) file. This file is automatically loaded when running. If running within Docker, this file is specified as an `env_file` in the [docker-compose](/docker-compose.yml) file, and loaded [by a script](/app/src/util/local.py) automatically when running unit tests (see running natively above for other cases).
72102

73103
Any environment variables specified directly in the [docker-compose](/docker-compose.yml) file will take precedent over those specified in the [local.env](/app/local.env) file.
74104

0 commit comments

Comments
 (0)