Forum OpenACS Q&A: Race condition in starting nsd and postgres

I have a linux box which is configured to launch Postgres from
rc.local, and to launch nsd from /etc/inittab.

Since I converted to an ext3 filesystem, the reboot time
is much quicker, and there is a race condition now on reboot:
nsd launches before the postgres server has time to start.

Has anyone else had this problem, and what is the preferred way
to fix it? I could just put a "sleep" in the script that launches
postgres, but that seems too crude. I would rather make some
kind of wait loop in ACS' init sequence that polls the database until
it
is able to connect.

Collapse
Posted by Jun Yamog on
Hi Henry,

Our setup for our development and production linux boxes are:

- postgres is started by init.d scripts

- nsd is started by deamon tools.

Although now that you have brought this up it does make me think.  Since deamon tools is started in init by svscan it should start earlier than postgres.  But I have never thought of the race condition that you have stated since it never happened to me.  I hope somebody could make a sane explation why I haven't experienced it?

Collapse
Posted by Rich Graves on
Run something like this out of inittab. kill -0 does nothing to the process but it does verify that it exists.
#!/bin/sh
# Wait up to 5 minutes for postgres to start, then whine.
I=0
until kill -0 `cat /var/run/postmaster.pid` >> /dev/null 2>&1
do
  sleep 10
  I=`expr $I + 1`
  test $I == 30 && echo|mail -s "Hmm... we have a problem" houston
  test $I == 31 && exit 1
done
exec nsd -u aolserver ...
Collapse
Posted by Stephen . on
Depending on which Linux distribution you use, it may already come with a solution for controlling what get's started when, and in what order, the Sys V style init scripts. The Life With Qmail documentation has a neat solution combining daemon-tools and init scripts which I've been happy using.

http://www.lifewithqmail.org/lwq.html#start-qmail

Collapse
Posted by Don Baccus on
ext3 will slow down PG considerably, BTW ... journal writing isn't free.  Folks in PG-land did some benchmarking a few months back and the slowdown was noticable.