fix daemon start and cleanup; improve logging
This commit is contained in:
@@ -34,6 +34,16 @@ except:
|
||||
"""
|
||||
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
|
||||
|
||||
|
||||
@@ -215,6 +225,9 @@ def doupdate(conn, msgDict):
|
||||
|
||||
msg={'service': 'update', 'msg': fail if fail else "OK"}
|
||||
conns[conn].sendto(msg)
|
||||
if not fail:
|
||||
syslog.syslog(syslog.LOG_ERR, 'hc updates, fs = %s' % (len(code)))
|
||||
|
||||
return fail
|
||||
|
||||
|
||||
@@ -244,9 +257,16 @@ def doupdateone(code, csum):
|
||||
|
||||
|
||||
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)
|
||||
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():
|
||||
@@ -270,9 +290,15 @@ def process():
|
||||
except KeyboardInterrupt:
|
||||
running = False
|
||||
break
|
||||
except SystemExit:
|
||||
syslog.syslog(syslog.LOG_ERR, 'daemon exit, running=: %s' % running)
|
||||
if running:
|
||||
running = False
|
||||
break
|
||||
except:
|
||||
if running:
|
||||
syslogtrace('select')
|
||||
running = False
|
||||
break
|
||||
if verbose: print "process: r is %s" % str(r)
|
||||
for rfh in r[0]:
|
||||
@@ -307,6 +333,7 @@ def process():
|
||||
|
||||
def cleanup():
|
||||
global running
|
||||
if verbose: syslog.syslog(syslog.LOG_ERR, 'cleanup')
|
||||
running = False
|
||||
for conn in conns:
|
||||
msg={'shutdown': 1, 'acks': conns[conn].ackcount}
|
||||
@@ -317,9 +344,9 @@ def cleanup():
|
||||
|
||||
|
||||
def closeall():
|
||||
if verbose: syslog.syslog(syslog.LOG_ERR, 'closecall')
|
||||
for conn in conns:
|
||||
conns[conn].close()
|
||||
syslog.syslog(syslog.LOG_ERR, 'exit')
|
||||
|
||||
|
||||
msgonly=False
|
||||
@@ -485,14 +512,15 @@ if fdaemon:
|
||||
print "daemoinizing... %s" % os.getpid()
|
||||
context = daemon.DaemonContext(
|
||||
working_directory='/tmp',
|
||||
umask=0o002,
|
||||
umask=0o022,
|
||||
pidfile=pidfile,
|
||||
detach_process=True,
|
||||
# initgroups=False,
|
||||
)
|
||||
|
||||
context.signal_map = {
|
||||
signal.SIGTERM: cleanup,
|
||||
signal.SIGHUP: 'terminate',
|
||||
# signal.SIGHUP: cleanup,
|
||||
signal.SIGTERM: 'terminate',
|
||||
# signal.SIGUSR1: reload_program_config,
|
||||
}
|
||||
|
||||
@@ -506,11 +534,6 @@ if fdaemon:
|
||||
process()
|
||||
except:
|
||||
syslogtrace('process')
|
||||
if verbose: print "main: cleanup"
|
||||
cleanup()
|
||||
if dorestart:
|
||||
if verbose: print "main: restart"
|
||||
restart()
|
||||
|
||||
else:
|
||||
running = True
|
||||
@@ -523,7 +546,6 @@ else:
|
||||
if verbose: print "main: cleanup"
|
||||
cleanup()
|
||||
if dorestart:
|
||||
if verbose: print "main: restart"
|
||||
restart()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user