* Provide two utility functions in Upstart jobs: "ensure JOBNAME"

starts the given job and waits until it's running; "stop_check"
  checks that the current job hasn't been asked to stop.

svn path=/nixos/trunk/; revision=33214
This commit is contained in:
Eelco Dolstra 2012-03-17 19:12:33 +00:00
parent 07df536c42
commit ee6c9bb998
5 changed files with 52 additions and 7 deletions

View File

@ -89,8 +89,8 @@ in
preStart =
''
start portmap || true
start mountd || true
ensure portmap
ensure mountd
# Create a state directory required by NFSv4.
mkdir -p /var/lib/nfs/v4recovery
@ -104,7 +104,7 @@ in
postStart =
''
start statd || true
ensure statd
'';
};
@ -115,7 +115,7 @@ in
preStart =
''
start portmap || true
ensure portmap
mkdir -p /var/lib/nfs
touch /var/lib/nfs/rmtab

View File

@ -88,7 +88,7 @@ in
# Portmap forks into the background before it starts
# listening, so wait until its ready.
while ! nc -z localhost 111; do
if [[ "$(status)" =~ stop/ ]]; then exit; fi
stop_check
sleep 1
done
'';

View File

@ -45,6 +45,7 @@ let
${optionalString (job.preStart != "") ''
pre-start script
exec >> ${log} 2>&1
source ${jobHelpers}
${job.preStart}
end script
''}
@ -55,6 +56,7 @@ let
''
script
exec >> ${log} 2>&1
source ${jobHelpers}
${job.script}
end script
''
@ -71,6 +73,7 @@ let
${optionalString (job.postStart != "") ''
post-start script
exec >> ${log} 2>&1
source ${jobHelpers}
${job.postStart}
end script
''}
@ -83,6 +86,7 @@ let
optionalString (job.preStop != "") (assert hasMain; ''
pre-stop script
exec >> ${log} 2>&1
source ${jobHelpers}
${job.preStop}
end script
'')}
@ -90,6 +94,7 @@ let
${optionalString (job.postStop != "") ''
post-stop script
exec >> ${log} 2>&1
source ${jobHelpers}
${job.postStop}
end script
''}
@ -122,6 +127,46 @@ let
'';
# Shell functions for use in Upstart jobs.
jobHelpers = pkgs.writeText "job-helpers.sh"
''
# Ensure that an Upstart service is running.
ensure() {
local job="$1"
local status="$(status "$job")"
# If it's already running, we're happy.
[[ "$status" =~ start/running ]] && return 0
# If its current goal is to stop, start it.
[[ "$status" =~ stop/ ]] && { status="$(start "$job")" || true; }
# The "start" command is synchronous *if* the job is
# not already starting. So if somebody else started
# the job in parallel, the "start" above may return
# while the job is still starting. So wait until it
# is up or has failed.
while true; do
[[ "$status" =~ stop/ ]] && { echo "job $job failed to start"; return 1; }
[[ "$status" =~ start/running ]] && return 0
echo "waiting for job $job to start..."
sleep 1
status="$(status "$job")"
done
}
# Check whether the current job has been stopped. Used in
# post-start jobs to determine if they should continue.
stop_check() {
if [[ "$(status)" =~ stop/ ]]; then
echo "job asked to stop!"
return 1
fi
return 0
}
'';
jobOptions = {
name = mkOption {

View File

@ -184,7 +184,7 @@ in
# ${fstab}
${optionalString config.services.nfs.client.enable ''
start statd || true
ensure statd || true
''}
exec > /dev/console 2>&1

View File

@ -49,7 +49,7 @@ in
preStart =
''
start portmap || true
ensure portmap
mkdir -p /var/lib/nfs
mkdir -p /var/lib/nfs/sm
mkdir -p /var/lib/nfs/sm.bak