gerrit.sh: Wait until the daemon is serving requests

Don't display "OK" until the daemon is able to serve requests.
This permits subsequent scripts to rely on our exit status to know
whether or not Gerrit Code Review is ready.

We now wait up to 90 seconds for the server to come online before
we report "OK" or "FAILED" to the console and the caller.

Because its difficult to compute the exact network addresses that
the daemon will be listening on, we instead pass it a magic cookie
as part of the arguments.  When the daemon's network listeners are
accepting connections the main thread writes that cookie into the
logs/gerrit.run file.  The calling script polls that file until
the cookie shows up.

Bug: issue 451
Change-Id: Ic5018b347a5037221c3d26c16c71173f5071d244
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2010-02-24 14:46:44 -08:00
parent ad34e3b266
commit 6c8e9ede2d
3 changed files with 63 additions and 12 deletions

View File

@@ -42,6 +42,9 @@ import org.kohsuke.args4j.Option;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -73,6 +76,9 @@ public class Daemon extends SiteProgram {
@Option(name = "--console-log", usage = "Log to console (not $site_path/logs)") @Option(name = "--console-log", usage = "Log to console (not $site_path/logs)")
private boolean consoleLog; private boolean consoleLog;
@Option(name = "--run-id", usage = "Cookie to store in $site_path/logs/gerrit.run")
private String runId;
private final LifecycleManager manager = new LifecycleManager(); private final LifecycleManager manager = new LifecycleManager();
private Injector dbInjector; private Injector dbInjector;
private Injector cfgInjector; private Injector cfgInjector;
@@ -80,11 +86,16 @@ public class Daemon extends SiteProgram {
private Injector sshInjector; private Injector sshInjector;
private Injector webInjector; private Injector webInjector;
private Injector httpdInjector; private Injector httpdInjector;
private File runFile;
@Override @Override
public int run() throws Exception { public int run() throws Exception {
mustHaveValidSite(); mustHaveValidSite();
if (runId != null) {
runFile = new File(new File(getSitePath(), "logs"), "gerrit.run");
}
if (httpd == null) { if (httpd == null) {
httpd = !slave; httpd = !slave;
} }
@@ -119,13 +130,33 @@ public class Daemon extends SiteProgram {
} }
manager.start(); manager.start();
log.info("Gerrit Code Review " + myVersion() + " ready");
RuntimeShutdown.add(new Runnable() { RuntimeShutdown.add(new Runnable() {
public void run() { public void run() {
log.info("caught shutdown, cleaning up"); log.info("caught shutdown, cleaning up");
if (runId != null) {
runFile.delete();
}
manager.stop(); manager.stop();
} }
}); });
log.info("Gerrit Code Review " + myVersion() + " ready");
if (runId != null) {
try {
runFile.createNewFile();
runFile.setReadable(true, false);
FileOutputStream out = new FileOutputStream(runFile);
try {
out.write((runId + "\n").getBytes("UTF-8"));
} finally {
out.close();
}
} catch (IOException err) {
log.warn("Cannot write --run-id to " + runFile, err);
}
}
RuntimeShutdown.waitFor(); RuntimeShutdown.waitFor();
return 0; return 0;
} }

View File

@@ -99,6 +99,7 @@ public class LogFileCompressor implements Runnable {
return ErrorLogFile.LOG_NAME.equals(name) // return ErrorLogFile.LOG_NAME.equals(name) //
|| "sshd_log".equals(name) // || "sshd_log".equals(name) //
|| "httpd_log".equals(name) // || "httpd_log".equals(name) //
|| "gerrit.run".equals(name) //
|| name.endsWith(".pid"); || name.endsWith(".pid");
} }

View File

@@ -201,6 +201,7 @@ test -r "$GERRIT_CONFIG" || {
} }
GERRIT_PID="$GERRIT_SITE/logs/gerrit.pid" GERRIT_PID="$GERRIT_SITE/logs/gerrit.pid"
GERRIT_RUN="$GERRIT_SITE/logs/gerrit.run"
################################################## ##################################################
# Check for JAVA_HOME # Check for JAVA_HOME
@@ -360,6 +361,9 @@ case "$ACTION" in
test -z "$UID" && UID=`id -u` test -z "$UID" && UID=`id -u`
RUN_ID=$(date +%s).$$
RUN_ARGS="$RUN_ARGS --run-id=$RUN_ID"
if test 1 = "$START_STOP_DAEMON" && type start-stop-daemon >/dev/null 2>&1 if test 1 = "$START_STOP_DAEMON" && type start-stop-daemon >/dev/null 2>&1
then then
test $UID = 0 && CH_USER="-c $GERRIT_USER" test $UID = 0 && CH_USER="-c $GERRIT_USER"
@@ -368,12 +372,14 @@ case "$ACTION" in
-d "$GERRIT_SITE" \ -d "$GERRIT_SITE" \
-a "$RUN_EXEC" -- $RUN_Arg1 "$RUN_Arg2" $RUN_Arg3 $RUN_ARGS -a "$RUN_EXEC" -- $RUN_Arg1 "$RUN_Arg2" $RUN_Arg3 $RUN_ARGS
then then
sleep 1 : OK
if running "$GERRIT_PID" ; then
echo OK
else else
echo FAILED rc=$?
if test $rc = 127; then
echo >&2 "fatal: start-stop-daemon failed"
rc=1
fi fi
exit $rc
fi fi
else else
if test -f "$GERRIT_PID" ; then if test -f "$GERRIT_PID" ; then
@@ -381,7 +387,7 @@ case "$ACTION" in
echo "Already Running!!" echo "Already Running!!"
exit 1 exit 1
else else
rm -f "$GERRIT_PID" rm -f "$GERRIT_PID" "$GERRIT_RUN"
fi fi
fi fi
@@ -399,9 +405,22 @@ case "$ACTION" in
disown $PID disown $PID
echo $PID >"$GERRIT_PID" echo $PID >"$GERRIT_PID"
fi fi
echo OK
fi fi
TIMEOUT=90 # seconds
sleep 1
while running "$GERRIT_PID" && test $TIMEOUT -gt 0 ; do
if test "x$RUN_ID" = "x$(cat $GERRIT_RUN 2>/dev/null)" ; then
echo OK
exit 0
fi
sleep 2
let TIMEOUT=$TIMEOUT-2
done
echo FAILED
exit 1
;; ;;
stop) stop)
@@ -420,7 +439,7 @@ case "$ACTION" in
fi fi
fi fi
fi fi
rm -f "$GERRIT_PID" rm -f "$GERRIT_PID" "$GERRIT_RUN"
echo OK echo OK
else else
PID=`cat "$GERRIT_PID" 2>/dev/null` PID=`cat "$GERRIT_PID" 2>/dev/null`
@@ -431,7 +450,7 @@ case "$ACTION" in
let TIMEOUT=$TIMEOUT-1 let TIMEOUT=$TIMEOUT-1
done done
test $TIMEOUT -gt 0 || kill -9 $PID 2>/dev/null test $TIMEOUT -gt 0 || kill -9 $PID 2>/dev/null
rm -f "$GERRIT_PID" rm -f "$GERRIT_PID" "$GERRIT_RUN"
echo OK echo OK
fi fi
;; ;;