Skip to content

Commit 0d17635

Browse files
committed
Optimize DB connections and unify container boot with migrations
Add connection pooling (CONN_MAX_AGE=600, CONN_HEALTH_CHECKS) to avoid DNS re-resolution and Postgres handshakes on every request. Refactor entrypoint.sh to run migrations on all container boots (web, workers, scheduler) so
1 parent aee9aee commit 0d17635

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ RUN mkdir -p $DJANGO_STATIC_ROOT $DJANGO_MEDIA_ROOT \
3838

3939
EXPOSE 80
4040

41-
# Command to run uWSGI
42-
CMD ["/bin/sh", "/entrypoint.sh"]
41+
# entrypoint.sh runs migrations, then either execs uWSGI (no args) or the
42+
# command passed via Compose `command:` (workers, scheduler, manage.py ...).
43+
ENTRYPOINT ["/bin/sh", "/entrypoint.sh"]

deploy/docker-compose.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ services:
122122
interval: 10s
123123
timeout: 5s
124124
retries: 5
125-
start_period: 60s
125+
# Generous window: every Django container now runs `migrate` on boot
126+
# (via entrypoint.sh) and only one acquires Django's advisory lock;
127+
# the others block. With three services and a growing schema, cold
128+
# boot can take >60s.
129+
start_period: 180s
126130

127131
task-worker:
128132
image: *landolfio-image

entrypoint.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
#!/bin/sh
2+
# Common boot for every Django container: web, task-worker, task-scheduler.
3+
# Apply migrations first so workers don't race the schema. Then dispatch:
4+
# - no argv → web (uWSGI)
5+
# - any argv → exec it (worker / scheduler / one-off command)
6+
set -e
7+
28
python manage.py migrate --noinput
39

10+
if [ "$#" -gt 0 ]; then
11+
exec "$@"
12+
fi
13+
414
# Ensure the uWSGI logfile exists so tail -F doesn't print a spurious warning.
515
touch /var/log/uwsgi.log
616

717
# Stream uWSGI logs to stdout for `docker logs`.
818
tail -F /var/log/uwsgi.log &
919

10-
# Start uWSGI (logs go to both file and stdout)
11-
uwsgi --http :80 \
20+
exec uwsgi --http :80 \
1221
--wsgi-file /app/website/wsgi.py \
1322
--master \
1423
--processes 4 \

landolfio/website/settings/production.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
"PASSWORD": os.environ.get("POSTGRES_PASSWORD"),
4343
"HOST": os.environ.get("POSTGRES_HOST"),
4444
"PORT": os.environ.get("POSTGRES_PORT"),
45+
# Keep DB connections warm so we don't re-resolve `database` DNS
46+
# and re-handshake Postgres on every request. Health-checks the
47+
# cached connection cheaply before reuse, so a stale socket from
48+
# a Postgres restart drops cleanly instead of erroring out.
49+
"CONN_MAX_AGE": 600,
50+
"CONN_HEALTH_CHECKS": True,
4551
}
4652
}
4753

0 commit comments

Comments
 (0)