Author: tmckay
Date: 2011-02-21 19:30:33 +0000 (Mon, 21 Feb 2011)
New Revision: 4540
Modified:
trunk/cumin/bin/cumin
Log:
Add logging to $CUMIN_HOME/master.log for startup errors,
informational messages on application start, warnings on restart.
Modified: trunk/cumin/bin/cumin
===================================================================
--- trunk/cumin/bin/cumin 2011-02-18 22:46:34 UTC (rev 4539)
+++ trunk/cumin/bin/cumin 2011-02-21 19:30:33 UTC (rev 4540)
@@ -6,7 +6,11 @@
import os
import sys
import signal
+from parsley.loggingex import *
+log = logging.getLogger("cumin.master")
+home = os.environ.get("CUMIN_HOME",
os.path.normpath("/usr/share/cumin"))
+
def call_sys_exit(sig,frame):
sys.exit()
@@ -21,6 +25,19 @@
else:
signal.signal(signal.SIGTERM, func)
+def log_parse_errors(r):
+ msg = ""
+ r = os.fdopen(r, "r")
+ while True:
+ t = r.read()
+ if t == "":
+ break
+ msg += t
+ r.close()
+ if msg:
+ msg = "\n"+msg
+ log.error("Error in options"+msg)
+
def main():
# tuple indices, for clarity
@@ -28,6 +45,8 @@
SECTION = 1
PROGRAM = 2
+ enable_logging("cumin.master", logging.INFO, os.path.join(home,
"master.log"))
+
parser = OptionParser()
parser.add_option("--init-only", dest="init_only",
action="store_true", default=False)
@@ -40,12 +59,26 @@
help="Configuration section names for cumin-data
instances."\
"\nEach value implies a separate cumin-data
instance.")
- (options, args) = parser.parse_args()
+ # Trap exit from parser and save standard error for logging
+ # Then put stderr back to original value
+ err = sys.stderr
+ r, w = os.pipe()
+ sys.stderr = os.fdopen(w,"w")
+ try:
+ (options, args) = parser.parse_args()
+ except:
+ parse_errors = True
+ else:
+ parse_errors = False
+ sys.stderr.close()
+ sys.stderr = err
+
+ if parse_errors:
+ log_parse_errors(r)
+ return 1
+
if len(args) != 0:
- sys.stderr.write("cumin: extra args: ")
- for a in args:
- sys.stderr.write(a+" ")
- sys.stderr.write("\n")
+ log.error("Extra arguments:" + "".join([" "+arg for
arg in args]))
return 1
# Get our list of cumin-web and data instances
@@ -58,14 +91,18 @@
apps.append([None, app, "cumin-data"])
# If we are just checking startup flags, invoke each instance
- # with "--init-only" and return status to caller
+ # with "--init-only" and return status to caller. Log failure with
+ # content of stderr from subprocess, if any.
if options.init_only:
for app in apps:
arg = [app[PROGRAM], "--init-only", "--section=" +
app[SECTION]]
- app[PROCESS] = subprocess.Popen(arg)
- exit_code = app[PROCESS].wait()
- if exit_code != 0:
- sys.stderr.write("cumin: --init-only check failed\n")
+ app[PROCESS] = subprocess.Popen(arg, stderr=subprocess.PIPE)
+ errors = app[PROCESS].communicate()[1]
+ if app[PROCESS].wait() != 0:
+ msg = "Subprocess failed:" + "".join([" "+x
for x in arg])
+ if len(errors) > 0:
+ msg += "\n" + errors
+ log.error(msg)
return 1
else:
# Launch and babysit
@@ -74,6 +111,11 @@
for app in apps:
if not app[PROCESS] or app[PROCESS].poll():
arg = [app[PROGRAM], "--section=" + app[SECTION]]
+ prog_string = "".join([" "+x for x in arg])
+ if app[PROCESS]:
+ log.warn("Restarting:" + prog_string)
+ else:
+ log.info("Starting:" + prog_string)
app[PROCESS] = subprocess.Popen(arg)
sleep(30)
Show replies by date