wait-for-started.conf-- a generic job that waits for a job to start
Clint Byrum
clint at ubuntu.com
Tue Feb 1 01:15:37 UTC 2011
So in the course of discussing the problem of synchronizing with
multiple events (aka "the state problem") with people, I've had to write
this example job a few times:
# foo-wait
start on X and Y
stop on started foo or stopped foo
instance $WAITER
normal exit 2
task
script
status foo | grep -q "start/running" && exit 0
start foo || true
while sleep 3600 ; do : ; done
end script
#EOF
This type of job was used to fix statd's startup in Ubuntu so that it
blocks mountall from mounting NFS shares until statd is running, and
also waits for local filesystems so it can deal with sm-notify's
requirements, and also waits for portmap to be fully started.
I understand that, going forward, we'll have the state rewrite, and that
will handle these situations properly.
However, until then, I think we can actually condense this, into the
following upstart job:
# wait-for-state
stop on started $WAITFOR or stopped $WAITFOR
normal exit 2
task
env WAITER=
env GOAL="start"
env TIMEOUT=5
env WAITFOREVER=N
instance $WAITER$WAITFOR$GOAL
script
# already running..
if [ x$GOAL = xstop ] ; then
target_state="waiting"
else
target_state="running"
fi
status $WAITFOR | grep -q "$GOAL/$target_state" && exit 0
# Make sure WAITFOR is an actual job..
status $WAITFOR | grep -q "^$WAITFOR" || exit 1
# Ok, try to start/stop it
$GOAL $WAITFOR || true
if [ x$WAITFOREVER = xY ] ; then
while sleep 3600 ; do : ; done
else
sleep $TIMEOUT && exit 1
fi
end script
# EOF
So, if anything needs to happen after a job reaches start/running or
stop/waiting, one only needs to precede it with this:
start wait-for-state WAITFOR=foo WAITFOREVER=Y GOAL=start WAITER=$UPSTART_JOB
This, unfortunately, doesn't work for jobs that rely on variables being
passed in to get them started properly.
I added in the WAITFOREVER and TIMEOUT's because I figure it may be
useful for instances where its possible the state will never be reached.
Having the default *not* to wait forever should help a user makig use of
this to decide when they want to wait forever.
I wanted to solicit any opinions on this before submitting this for
inclusion in the Ubuntu upstart package (and possibly patching nfs-utils
and portmap to use this method).
More information about the upstart-devel
mailing list