Skip to content

Commit 0118862

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. Add a disabled postgres-config service to run create_db, and run once after postgres starts
1 parent 64f31db commit 0118862

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

templates/postgres.template.yml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,32 @@ 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:
22+
- file:
23+
path: /etc/service/postgres-config/run
24+
chmod: "+x"
25+
contents: |
26+
#!/bin/sh
27+
exec 2>&1
28+
if [ "$CREATE_DB_ON_BOOT" = "1" ]; then
29+
/usr/local/bin/create_db
30+
fi
31+
- file:
32+
path: /etc/service/postgres-config/down
33+
2634
- file:
2735
path: /etc/service/postgres/run
2836
chmod: "+x"
2937
contents: |
3038
#!/bin/sh
3139
exec 2>&1
40+
if [ -f /root/install_postgres ]; then
41+
/root/install_postgres
42+
rm /root/install_postgres
43+
fi
44+
sv once postgres-config
3245
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
3346
3447
- file:
@@ -242,6 +255,11 @@ run:
242255
chmod: +x
243256
contents: |
244257
#!/bin/bash
258+
# wait for postgres to start up...
259+
for i in {1..5}; do
260+
pg_isready -U postgres -q && break
261+
sleep 1
262+
done
245263
su postgres -c 'createdb $db_name' || true
246264
su postgres -c 'psql $db_name -c "create user $db_user;"' || true
247265
su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
@@ -278,7 +296,5 @@ run:
278296
tag: db
279297
hook: postgres
280298
cmd:
281-
# give db a few secs to start up
282-
- "sleep 5"
283299
- /usr/local/bin/create_db
284300
- "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)