Skip to content

Commit 136aefe

Browse files
Move DB initialization scripts for postgres and redis into service files.
This resolves a race condition with unconfigured images attempting to bring up DBs for the first time. This does not affect fully bootstrapped images. Currently, all jobs start at boot - this includes postgres. Issue with the current is - postgres starts and adds the corresponding .s/.pid files to /var/run/postgres. Simultaneously, the unicorn job gets started, checks to see if postgres is running (it is already at this point from boot), and runs install_postgres. Inside the install_postgres script, we mount the shared postgres folder and remove .s/.pid files -- after postgres has already been started. In this case, we remove the (in-use) .s and .pid files. Subsequent unicorn tasks fail, erroring out the service and forcing it into a restart loop. Since postgres never restarts, it never regenerates the .s/.pid files, and unicorn can never run successfully. This proposal moves install_postgres into the postgres job file, eliminating the race condition. Since they are part of the same service, install_postgres will always run before starting postgres - it will no longer be able to remove valid .s and .pid files. Redis has a similar race condition with the creation of its data folder. This isn't as disastrous as the redis service restarts until the folder exists from unicorn run, but it provides better reasoning about the running services. Add early exit from unicorn boot scripts to properly retry migrate as well. Use pg_isready to check if pg is ready directly in create_db. Merge the ready check into create_db. Run create_db in a subshell on postgres job start, rather than in unicorn script. remove postgres-config call
1 parent 64f31db commit 136aefe

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

templates/postgres.template.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ hooks:
1616
filename: /etc/service/unicorn/run
1717
from: "# postgres"
1818
to: |
19-
if [ -f /root/install_postgres ]; then
20-
/root/install_postgres
21-
rm /root/install_postgres
22-
fi
2319
sv start postgres || exit 1
2420
2521
run:
@@ -29,6 +25,13 @@ run:
2925
contents: |
3026
#!/bin/sh
3127
exec 2>&1
28+
if [ -f /root/install_postgres ]; then
29+
/root/install_postgres
30+
rm /root/install_postgres
31+
fi
32+
if [ "$CREATE_DB_ON_BOOT" = "1" ]; then
33+
/usr/local/bin/create_db&
34+
fi
3235
HOME=/var/lib/postgresql USER=postgres exec thpoff chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/15/bin/postmaster -D /etc/postgresql/15/main
3336
3437
- file:
@@ -242,6 +245,11 @@ run:
242245
chmod: +x
243246
contents: |
244247
#!/bin/bash
248+
# wait for postgres to start up...
249+
for i in {1..5}; do
250+
su postgres -c 'pg_isready -q' && break
251+
sleep 1
252+
done
245253
su postgres -c 'createdb $db_name' || true
246254
su postgres -c 'psql $db_name -c "create user $db_user;"' || true
247255
su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
@@ -278,7 +286,5 @@ run:
278286
tag: db
279287
hook: postgres
280288
cmd:
281-
# give db a few secs to start up
282-
- "sleep 5"
283289
- /usr/local/bin/create_db
284290
- "echo postgres installed!"

templates/redis.template.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ run:
99
contents: |
1010
#!/bin/sh
1111
exec 2>&1
12+
if [ ! -d /shared/redis_data ]; then
13+
install -d -m 0755 -o redis -g redis /shared/redis_data
14+
fi
1215
exec thpoff chpst -u redis -U redis /usr/bin/redis-server /etc/redis/redis.conf
1316
- file:
1417
path: /etc/service/redis/log/run
@@ -88,7 +91,4 @@ hooks:
8891
filename: /etc/service/unicorn/run
8992
from: "# redis"
9093
to: |
91-
if [ ! -d /shared/redis_data ]; then
92-
install -d -m 0755 -o redis -g redis /shared/redis_data
93-
fi
9494
sv start redis || exit 1

templates/web.template.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ run:
6161
if [[ -z "$PRECOMPILE_ON_BOOT" ]]; then
6262
PRECOMPILE_ON_BOOT=1
6363
fi
64-
if [ -f /usr/local/bin/create_db ] && [ "$CREATE_DB_ON_BOOT" = "1" ]; then /usr/local/bin/create_db; fi;
65-
if [ "$MIGRATE_ON_BOOT" = "1" ]; then su discourse -c 'bundle exec rake db:migrate'; fi
66-
if [ "$PRECOMPILE_ON_BOOT" = "1" ]; then SKIP_EMBER_CLI_COMPILE=1 su discourse -c 'bundle exec rake assets:precompile'; fi
64+
if [ "$MIGRATE_ON_BOOT" = "1" ]; then su discourse -c 'bundle exec rake db:migrate' || exit 1; fi
65+
if [ "$PRECOMPILE_ON_BOOT" = "1" ]; then SKIP_EMBER_CLI_COMPILE=1 su discourse -c 'bundle exec rake assets:precompile' || exit 1; fi
6766
LD_PRELOAD=$RUBY_ALLOCATOR HOME=/home/discourse USER=discourse exec thpoff chpst -u discourse:www-data -U discourse:www-data bundle exec config/unicorn_launcher -E production -c config/unicorn.conf.rb
6867
6968
- file:

0 commit comments

Comments
 (0)