Add more debugging when moltenirond-helper fails
Added custom Exception classes and raised them in get_moltenirond_pid. Added log_error to log to stderr and a logfile called /tmp/MoltenIron-error-logfile. Change-Id: I2855cf832c2a1a8f5ef9df34cf96706eb5406276
This commit is contained in:
parent
eadfe8ec0f
commit
707f272eb8
|
@ -25,10 +25,20 @@ from molteniron import moltenirond
|
|||
|
||||
PID = "/var/run/moltenirond.pid"
|
||||
YAML_CONF = "/usr/local/etc/molteniron/conf.yaml"
|
||||
ERROR_LOGFILE = "/tmp/MoltenIron-error-logfile"
|
||||
|
||||
class MoltenIronPIDNotFound(RuntimeError):
|
||||
"""os.path.isfile() error: The PID file does not exist"""
|
||||
|
||||
class MoltenIronKillError(RuntimeError):
|
||||
"""os.kill() error"""
|
||||
|
||||
class MoltenIronReadLinesError(RuntimeError):
|
||||
"""fobj.readlines() error"""
|
||||
|
||||
def get_moltenirond_pid():
|
||||
if not os.path.isfile(PID):
|
||||
return -1
|
||||
raise MoltenIronPIDNotFound("isfile error %s" % (PID, ))
|
||||
|
||||
with open(PID) as fobj:
|
||||
lines = fobj.readlines()
|
||||
|
@ -38,12 +48,12 @@ def get_moltenirond_pid():
|
|||
try:
|
||||
# Send harmless kill signal in order to test existance
|
||||
os.kill(pid, 0)
|
||||
except Exception:
|
||||
return -1
|
||||
except Exception as e:
|
||||
raise MoltenIronKillError("os.kill error: %s" % (e, ))
|
||||
|
||||
return pid
|
||||
except Exception:
|
||||
return -1
|
||||
except Exception as e:
|
||||
raise MoltenIronReadLinesError("readlines error: %s" % (e, ))
|
||||
|
||||
def moltenirond_main():
|
||||
with open(YAML_CONF, "r") as fobj:
|
||||
|
@ -51,6 +61,11 @@ def moltenirond_main():
|
|||
|
||||
moltenirond.listener(conf)
|
||||
|
||||
def log_error(s):
|
||||
with open(ERROR_LOGFILE, "a+") as fobj:
|
||||
fobj.writelines(s + "\n")
|
||||
print >> sys.stderr, s
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
parser = argparse.ArgumentParser(description="Molteniron daemon helper")
|
||||
|
@ -98,27 +113,36 @@ if __name__ == "__main__":
|
|||
|
||||
if len(args.command) != 1:
|
||||
msg = "Error: Expecting one command? Received: %s" % (args.command, )
|
||||
print >> sys.stderr, msg
|
||||
log_error(msg)
|
||||
sys.exit (1)
|
||||
|
||||
if args.command[0].upper().lower() == "start":
|
||||
pid = get_moltenirond_pid()
|
||||
try:
|
||||
pid = get_moltenirond_pid()
|
||||
except MoltenIronPIDNotFound:
|
||||
pid = -1
|
||||
if pid > 0:
|
||||
print >> sys.stderr, "Error: The daemon is already running"
|
||||
log_error("Error: The daemon is already running")
|
||||
sys.exit(1)
|
||||
daemon = Daemonize(app="moltenirond",
|
||||
pid=PID,
|
||||
action=moltenirond_main)
|
||||
daemon.start()
|
||||
elif args.command[0].upper().lower() == "stop":
|
||||
pid = get_moltenirond_pid()
|
||||
if pid > 0:
|
||||
os.remove (PID)
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
else:
|
||||
print >> sys.stderr, "Error: The daemon doesn't exist?"
|
||||
try:
|
||||
pid = get_moltenirond_pid()
|
||||
if pid > 0:
|
||||
os.remove (PID)
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
else:
|
||||
log_error("Error: The daemon doesn't exist?")
|
||||
log_error("Error: pid = %d" % (pid, ))
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
log_error("Error: The daemon doesn't exist?")
|
||||
log_error("Error: %s" % (e, ))
|
||||
sys.exit(1)
|
||||
else:
|
||||
msg = "Error: Unknown command: %s" % (args.command[0], )
|
||||
print >> sys.stderr, msg
|
||||
log_error(msg)
|
||||
sys.exit (1)
|
||||
|
|
Loading…
Reference in New Issue