couchdb (0.11.0-2.3) init.patch

Summary

 etc/init/couchdb.tpl.in |   46 +++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 39 insertions(+), 7 deletions(-)

    
download this patch

Patch contents

--- couchdb-0.10.1/etc/init/couchdb.tpl.in	2009-07-14 16:21:39.000000000 -0400
+++ couchdb-0.10.1.new/etc/init/couchdb.tpl.in	2010-03-08 22:18:35.000000000 -0500
@@ -33,8 +33,12 @@
 RUN_DIR=%localstaterundir%
 LSB_LIBRARY=/lib/lsb/init-functions
 
+# Grace time in seconds to give the beam process to stop when we're running the
+# stop target. Will end before grace time ends if the process ends sooner.
+STOP_GRACE_TIME=5
+
 if test ! -x $COUCHDB; then
-    exit $SCRIPT_ERROR
+    exit $SCRIPT_OK
 fi
 
 if test -r $CONFIGURATION_FILE; then
@@ -77,6 +81,7 @@
         command="$command $COUCHDB_OPTIONS"
     fi
     mkdir -p "$RUN_DIR"
+    cd "$RUN_DIR"
     if test -n "$COUCHDB_USER"; then
         chown $COUCHDB_USER "$RUN_DIR"
         if su $COUCHDB_USER -c "$command" > /dev/null; then
@@ -96,23 +101,50 @@
 stop_couchdb () {
     # Stop the running Apache CouchDB process.
 
+    pidFile="$RUN_DIR/couchdb.pid"
+    if [ ! -r "$pidFile" ] 
+    then
+        #exists, but can't read it 
+        [ -f "$pidFile" ] && return $SCRIPT_ERROR
+
+        #doesn't exist, so assume couchdb is already stopped
+        return $SCRIPT_OK
+    fi
+
+    pid=`cat $pidFile` 
+    #unset $pidFile 
+    [ -z "$pid" ] && return $SCRIPT_OK
+
     command="$COUCHDB -d"
     if test -n "$COUCHDB_OPTIONS"; then
         command="$command $COUCHDB_OPTIONS"
     fi
+
+    # We need `heart`'s pid because its ppid is set to 1 when the beam proc
+    # ends, thereby hiding itself from our `ps` check bellow.
+    heart_pid=`ps -f --ppid $pid | grep "heart -pid $pid " | awk '{print $2}'`
+    [ -n "$heart_pid" ] && heart_pid=",$heart_pid" #for `ps` call formatting
+
     if test -n "$COUCHDB_USER"; then
-        if su $COUCHDB_USER -c "$command" > /dev/null; then
-            return $SCRIPT_OK
-        else
+        if ! su $COUCHDB_USER -c "$command" > /dev/null; then
             return $SCRIPT_ERROR
         fi
     else
-        if $command > /dev/null; then
-            return $SCRIPT_OK
-        else
+        if ! $command > /dev/null; then
             return $SCRIPT_ERROR
         fi
     fi
+
+    i=0
+    while ps -p $pid$heart_pid --ppid $pid$heart_pid > /dev/null 
+    do
+        [ $i -ge $STOP_GRACE_TIME ] && return $SCRIPT_ERROR
+
+        sleep 1
+        i=`expr $i + 1`
+    done
+
+    return $SCRIPT_OK
 }
 
 display_status () {