Author: tmckay
Date: 2012-05-17 19:55:39 +0000 (Thu, 17 May 2012)
New Revision: 5382
Modified:
trunk/cumin/bin/cumin
trunk/cumin/bin/cumin-checkpid
trunk/cumin/etc/sysvinit-cumin
Log:
More tweaks to sync between initd and cumin and management of pid files
BZ801287
Modified: trunk/cumin/bin/cumin
===================================================================
--- trunk/cumin/bin/cumin 2012-05-17 15:35:36 UTC (rev 5381)
+++ trunk/cumin/bin/cumin 2012-05-17 19:55:39 UTC (rev 5382)
@@ -55,9 +55,13 @@
syslog.syslog("cumin: master script exited with errors, see
%s/log/master.log" % home)
# Used to write the pidfile and the startup status files.
-def write_status_path(p, value):
+def write_status_path(p, value, append=False):
+ if append:
+ mode = "a+"
+ else:
+ mode = "w+"
try:
- f = open(p, "w+")
+ f = open(p, mode)
if value:
f.write(value)
f.close()
@@ -180,13 +184,13 @@
if not options:
v = log_parse_errors(r)
write_status_path(config.get_init_status_path(),
- str(v)+"\n")
+ str(v)+"\nexit\n")
return v
if len(args) != 0:
log.error("Extra arguments:" + "".join([" "+arg for
arg in args]))
_syslog.log()
- write_status_path(config.get_init_status_path(), "1\n")
+ write_status_path(config.get_init_status_path(), "1\nexit\n")
return 1
apps = []
@@ -219,7 +223,7 @@
init_files.append(init_path)
apps.append([None, args, prog_string, init_path])
- # Clear the init file
+ # Clear the init file for the app
write_status_path(init_path, "")
# Launch and babysit, do not restart if options.init_only is set
@@ -296,14 +300,14 @@
init_files.remove(app[INIT_FILE])
if len(init_files) == 0:
write_status_path(config.get_init_status_path(),
- "0"+"\n")
+ "0\n")
wrote_status = True
finally:
# Hmm, exiting, but we haven't written the status file yet.
# If we had seen an init failure from a child we would have
# written the file. Let's assume it was a success.
if not wrote_status:
- write_status_path(config.get_init_status_path(),
"0"+"\n")
+ write_status_path(config.get_init_status_path(), "0\n")
log.info("Write termination string to all children")
complete = 0
for app in apps:
@@ -342,7 +346,8 @@
log.warn("Subprocess (%s) failed to stop"\
% app[PROCESS].pid)
break
-
+
+ write_status_path(config.get_init_status_path(), "exit\n",
append=True)
return return_code
if __name__ == "__main__":
Modified: trunk/cumin/bin/cumin-checkpid
===================================================================
--- trunk/cumin/bin/cumin-checkpid 2012-05-17 15:35:36 UTC (rev 5381)
+++ trunk/cumin/bin/cumin-checkpid 2012-05-17 19:55:39 UTC (rev 5382)
@@ -3,27 +3,48 @@
import os
from datetime import datetime, timedelta
from time import sleep
+from optparse import OptionParser
home = os.environ.get("CUMIN_HOME",
os.path.normpath("/usr/share/cumin"))
sys.path.append(os.path.join(home, "python"))
from cumin.config import CuminMasterConfig
+def read_exit(p, then, timeout):
+ while True:
+ try:
+ f = open(p, "r")
+ # Get the last line of the file
+ v = f.readlines()[-1:]
+ if len(v) and v[0][:-1] == "exit":
+ return 0
+ if datetime.now() - then > timedelta(seconds=timeout):
+ return -1
+ sleep(0.25)
+ except:
+ return -1
+
def read_val(p, then, timeout):
- while True:
+ while True:
try:
f = open(p, "r")
v = f.readline()
if v:
- return int(v[:-1])
+ return int(v[:-1])
if datetime.now() - then > timedelta(seconds=timeout):
- return None
+ return None
sleep(0.25)
except:
return None
def main():
+ parser = OptionParser()
+ parser.add_option("--exit", dest="exit",
action="store_true", default=False,
+ help="Wait for master init to indicate exit")
+
+ (options, args) = parser.parse_args()
+
# Wait for the pidfile to exist.
# When called from service this should be immediate
then = datetime.now()
@@ -31,9 +52,8 @@
sleep(0.25)
if datetime.now() - then > timedelta(seconds=5):
return -1
-
- # We need a value written to the pid file otherwise
- # assume the service failed
+
+ # Wait for a value
if not read_val("/var/run/cumin.pid", then, 5):
return -1
@@ -46,7 +66,13 @@
if datetime.now() - then > timedelta(seconds=5):
return -1
- # Okay, now let's read the status. If there is no
+ # We don't care about the status value here, we
+ # just want to see "exit" on the last line of the
+ # file
+ if options.exit:
+ return read_exit(p, then, 5)
+
+ # Let's read the status. If there is no
# value there, assume success.
status = read_val(p, then, 10)
if status is None:
Modified: trunk/cumin/etc/sysvinit-cumin
===================================================================
--- trunk/cumin/etc/sysvinit-cumin 2012-05-17 15:35:36 UTC (rev 5381)
+++ trunk/cumin/etc/sysvinit-cumin 2012-05-17 19:55:39 UTC (rev 5382)
@@ -94,6 +94,13 @@
# Tidy up the banner
if [ $RETVAL -ne 0 ] ; then
+ # We want to remove the pidfile when the
+ # cumin master indicates exit
+ /usr/sbin/cumin-checkpid --exit
+ if [ $? = 0 ]; then
+ rm -f $lockfile
+ rm -f $pidfile
+ fi
echo_failure; echo; return $RETVAL
fi
echo_success && echo
@@ -103,12 +110,23 @@
stop() {
echo -n $"Stopping cumin: "
- killproc -d $(($DELAY-1)) $servicename
+ # if we think it's running we'll wait before clearing the pidfile
+ status $processname -p $pidfile -l $lockfile &> /dev/null
+ running=$?
+
+ killproc -d $(($DELAY-1)) $servicename SIGTERM
RETVAL=$?
- echo
+ echo
if [ $RETVAL -eq 0 ]; then
- rm -f /var/lock/subsys/$servicename
- rm -f /var/run/cumin.pid
+ if [ $running -eq 0 ]; then
+ /usr/sbin/cumin-checkpid --exit
+ if [ $? -eq 0 ]; then
+ rm -f $lockfile
+ rm -f $pidfile
+ else
+ echo "Timed out, cumin may not have stopped completely."
+ fi
+ fi
else
if [[ "$EUID" != "0" ]]; then
echo "Must be run as root"