restart on sig 1

This commit is contained in:
andreas
2006-11-07 20:21:43 +00:00
parent f5e130f638
commit 4c68b9dea4
+37 -9
View File
@@ -1,14 +1,18 @@
#!/usr/bin/env python #!/usr/bin/env python
# $Id: hbd,v 1.12 2006/11/07 19:41:11 andreas Exp $ # $Id: hbd,v 1.13 2006/11/07 20:21:43 andreas Exp $
# Wait for heartbeat messages and act on them (or their absence) # Wait for heartbeat messages and act on them (or their absence)
# #
import time, os, string, sys, socket, atexit, select, SocketServer, getopt, signal import time, os, string, sys, socket, atexit, select, SocketServer, getopt, signal, cPickle
False=0 False=0
True=1 True=1
LOGFILE="/home/andreas/public_html/messages/andreas" LOGFILE="/home/andreas/public_html/messages/andreas"
hosts={} hosts={}
htab={}
msgs=[]
num=0 num=0
upcount=0 upcount=0
PORT=50003 PORT=50003
@@ -22,7 +26,6 @@ GRACE=2
visual=0 visual=0
msgs=[]
stdscr=None stdscr=None
win=None win=None
@@ -33,9 +36,11 @@ msgwHeight=10
def handler(signum, frame): def handler(signum, frame):
global up, sig global up, sig
sig=signum sig=signum
if verbose: print "signal: %s up: %d" % (sig, up)
if up == 0: if up == 0:
return return
sys.exit(0) up=0
# sys.exit(0)
class NullDevice: class NullDevice:
def write(self, s): def write(self, s):
@@ -205,7 +210,7 @@ def displayaddr(h, refresh=1):
# #
def displaybody(): def displaybody():
for h in hosts.keys(): for h in hosts.keys():
win.addstr(hosts[h].num+1, 1, "%-24s" % (h)) win.addstr(hosts[h].num+1, 1, "%-25s" % (h))
if hosts[h].addr != None: if hosts[h].addr != None:
displayaddr(h, 0) displayaddr(h, 0)
if hosts[h].statetime != None: if hosts[h].statetime != None:
@@ -423,12 +428,13 @@ class HtmlHandler(SocketServer.BaseRequestHandler):
helpflag=False helpflag=False
forground=False forground=False
restart=None
optlist=[] optlist=[]
args=[] args=[]
home=os.environ['HOME'] home=os.environ['HOME']
configfile="%s/.hbrc" % home configfile="%s/.hbrc" % home
try: try:
optlist, args = getopt.getopt(sys.argv[1:], 'c:dfhv') optlist, args = getopt.getopt(sys.argv[1:], 'c:dfhR:v')
except: except:
helpflag=True helpflag=True
@@ -441,6 +447,8 @@ for o,a in optlist:
forground=True forground=True
elif o == '-h': elif o == '-h':
helpflag=True helpflag=True
elif o == '-R':
restart=a
elif o == '-v': elif o == '-v':
verbose=True verbose=True
@@ -521,17 +529,26 @@ if len(args) != 0:
if verbose: print "notice: logging to %s" % logfile if verbose: print "notice: logging to %s" % logfile
logf=initlog(logfile) logf=initlog(logfile)
if restart:
pickf=open(restart, 'r')
pick=cPickle.Unpickler(pickf)
hosts=pick.load()
htab=pick.load()
msgs=pick.load()
pickf.close()
now=time.time() now=time.time()
startsec=int(now) % interval startsec=int(now) % interval
htab={}
if visual: if visual:
import curses import curses
initcurses() initcurses()
display() display()
stdscr.nodelay(1) stdscr.nodelay(1)
if verbose: log("Starting") if verbose:
if restart: log("Restarting")
else: log("Starting")
atexit.register(on_exit) atexit.register(on_exit)
ilist=[] ilist=[]
@@ -544,6 +561,7 @@ serv=SocketServer.TCPServer((hbd_host,hbd_port),HtmlHandler)
ilist.append(serv.fileno()) ilist.append(serv.fileno())
if not forground: if not forground:
if not restart:
pid=os.fork() pid=os.fork()
if pid > 0: if pid > 0:
if verbose: if verbose:
@@ -616,4 +634,14 @@ while up:
if sig == signal.SIGHUP: if sig == signal.SIGHUP:
os.execv(sys.argv[0],sys.argv+['-R']) sock.close()
serv.socket.close()
pickf=open("/tmp/pick1", 'w')
pick=cPickle.Pickler(pickf)
pick.dump(hosts)
pick.dump(htab)
pick.dump(msgs)
pickf.close()
os.execv(sys.argv[0],sys.argv+['-R', "/tmp/pick1"])