Skip to content

Commit 86c23d5

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 runtime sleep before create_db is run to better mirror a pups run Add early exit from unicorn boot scripts for faster service restarts.
1 parent 64f31db commit 86c23d5

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

templates/postgres.template.yml

Lines changed: 4 additions & 4 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,10 @@ 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
3232
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
3333
3434
- file:

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ 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 [ -f /usr/local/bin/create_db ] && [ "$CREATE_DB_ON_BOOT" = "1" ]; then sleep 5; /usr/local/bin/create_db || exit 1; fi;
65+
if [ "$MIGRATE_ON_BOOT" = "1" ]; then su discourse -c 'bundle exec rake db:migrate' || exit 1; fi
66+
if [ "$PRECOMPILE_ON_BOOT" = "1" ]; then SKIP_EMBER_CLI_COMPILE=1 su discourse -c 'bundle exec rake assets:precompile' || exit 1; fi
6767
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
6868
6969
- file:

0 commit comments

Comments
 (0)