fix daemon start and cleanup; improve logging
This commit is contained in:
@@ -34,6 +34,16 @@ except:
|
|||||||
"""
|
"""
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
# N.B. daemon tries to close resource.RLIMIT_NOFILE file descriptors
|
||||||
|
# which on FreeBSD in close to a million
|
||||||
|
# hack: replace the function in daemon with ths one:
|
||||||
|
|
||||||
|
def get_maximum_file_descriptors():
|
||||||
|
return 2048
|
||||||
|
|
||||||
|
daemon.get_maximum_file_descriptors = get_maximum_file_descriptors
|
||||||
|
|
||||||
import syslog
|
import syslog
|
||||||
|
|
||||||
|
|
||||||
@@ -215,6 +225,9 @@ def doupdate(conn, msgDict):
|
|||||||
|
|
||||||
msg={'service': 'update', 'msg': fail if fail else "OK"}
|
msg={'service': 'update', 'msg': fail if fail else "OK"}
|
||||||
conns[conn].sendto(msg)
|
conns[conn].sendto(msg)
|
||||||
|
if not fail:
|
||||||
|
syslog.syslog(syslog.LOG_ERR, 'hc updates, fs = %s' % (len(code)))
|
||||||
|
|
||||||
return fail
|
return fail
|
||||||
|
|
||||||
|
|
||||||
@@ -244,9 +257,16 @@ def doupdateone(code, csum):
|
|||||||
|
|
||||||
|
|
||||||
def restart():
|
def restart():
|
||||||
if verbose: print "restart: execv %s %s" % (sys.argv[0], [sys.argv[0]]+cmdargs)
|
if verbose:
|
||||||
|
print "restart: execv %s %s" % (sys.argv[0], [sys.argv[0]]+cmdargs)
|
||||||
|
syslog.syslog(syslog.LOG_ERR, 'restart %s' % (sys.argv[0]))
|
||||||
|
e = "fallthrough"
|
||||||
|
try:
|
||||||
os.execv(sys.argv[0], [sys.argv[0]]+cmdargs)
|
os.execv(sys.argv[0], [sys.argv[0]]+cmdargs)
|
||||||
print "should not be here"
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
print "should not be here:", str(e)
|
||||||
|
syslog.syslog(syslog.LOG_ERR, 'restart failed: %s' % e)
|
||||||
|
|
||||||
|
|
||||||
def process():
|
def process():
|
||||||
@@ -270,9 +290,15 @@ def process():
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
running = False
|
running = False
|
||||||
break
|
break
|
||||||
|
except SystemExit:
|
||||||
|
syslog.syslog(syslog.LOG_ERR, 'daemon exit, running=: %s' % running)
|
||||||
|
if running:
|
||||||
|
running = False
|
||||||
|
break
|
||||||
except:
|
except:
|
||||||
if running:
|
if running:
|
||||||
syslogtrace('select')
|
syslogtrace('select')
|
||||||
|
running = False
|
||||||
break
|
break
|
||||||
if verbose: print "process: r is %s" % str(r)
|
if verbose: print "process: r is %s" % str(r)
|
||||||
for rfh in r[0]:
|
for rfh in r[0]:
|
||||||
@@ -307,6 +333,7 @@ def process():
|
|||||||
|
|
||||||
def cleanup():
|
def cleanup():
|
||||||
global running
|
global running
|
||||||
|
if verbose: syslog.syslog(syslog.LOG_ERR, 'cleanup')
|
||||||
running = False
|
running = False
|
||||||
for conn in conns:
|
for conn in conns:
|
||||||
msg={'shutdown': 1, 'acks': conns[conn].ackcount}
|
msg={'shutdown': 1, 'acks': conns[conn].ackcount}
|
||||||
@@ -317,9 +344,9 @@ def cleanup():
|
|||||||
|
|
||||||
|
|
||||||
def closeall():
|
def closeall():
|
||||||
|
if verbose: syslog.syslog(syslog.LOG_ERR, 'closecall')
|
||||||
for conn in conns:
|
for conn in conns:
|
||||||
conns[conn].close()
|
conns[conn].close()
|
||||||
syslog.syslog(syslog.LOG_ERR, 'exit')
|
|
||||||
|
|
||||||
|
|
||||||
msgonly=False
|
msgonly=False
|
||||||
@@ -485,14 +512,15 @@ if fdaemon:
|
|||||||
print "daemoinizing... %s" % os.getpid()
|
print "daemoinizing... %s" % os.getpid()
|
||||||
context = daemon.DaemonContext(
|
context = daemon.DaemonContext(
|
||||||
working_directory='/tmp',
|
working_directory='/tmp',
|
||||||
umask=0o002,
|
umask=0o022,
|
||||||
pidfile=pidfile,
|
pidfile=pidfile,
|
||||||
|
detach_process=True,
|
||||||
# initgroups=False,
|
# initgroups=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
context.signal_map = {
|
context.signal_map = {
|
||||||
signal.SIGTERM: cleanup,
|
# signal.SIGHUP: cleanup,
|
||||||
signal.SIGHUP: 'terminate',
|
signal.SIGTERM: 'terminate',
|
||||||
# signal.SIGUSR1: reload_program_config,
|
# signal.SIGUSR1: reload_program_config,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -506,11 +534,6 @@ if fdaemon:
|
|||||||
process()
|
process()
|
||||||
except:
|
except:
|
||||||
syslogtrace('process')
|
syslogtrace('process')
|
||||||
if verbose: print "main: cleanup"
|
|
||||||
cleanup()
|
|
||||||
if dorestart:
|
|
||||||
if verbose: print "main: restart"
|
|
||||||
restart()
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
running = True
|
running = True
|
||||||
@@ -520,10 +543,9 @@ else:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
if verbose: print "err: process exit: %s" % e
|
if verbose: print "err: process exit: %s" % e
|
||||||
syslogtrace('process')
|
syslogtrace('process')
|
||||||
if verbose: print "main: cleanup"
|
if verbose: print "main: cleanup"
|
||||||
cleanup()
|
cleanup()
|
||||||
if dorestart:
|
if dorestart:
|
||||||
if verbose: print "main: restart"
|
|
||||||
restart()
|
restart()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user