replace homebrow lock with package lockfile
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# $Id: hbc,v 1.9 2012/03/29 02:08:36 andreas Exp $
|
# $Id: hbc,v 1.9 2012/03/29 02:08:36 andreas Exp $
|
||||||
|
|
||||||
|
# requre python-filelock on Linux
|
||||||
|
# require py27-lockfile on *bsd
|
||||||
|
# or run sudo easy_install-2.7 lockfile
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import socket
|
import socket
|
||||||
@@ -9,6 +13,8 @@ import getopt
|
|||||||
import string
|
import string
|
||||||
import select
|
import select
|
||||||
import errno
|
import errno
|
||||||
|
import traceback
|
||||||
|
from lockfile import FileLock
|
||||||
|
|
||||||
PORT=50003
|
PORT=50003
|
||||||
INTERVAL=10
|
INTERVAL=10
|
||||||
@@ -21,67 +27,6 @@ class NullDevice:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Flock:
|
|
||||||
def __init__(self, lock_file):
|
|
||||||
self.lock_file = lock_file
|
|
||||||
self.fd = None
|
|
||||||
self.opid = None
|
|
||||||
|
|
||||||
def lock(self):
|
|
||||||
if DBG: print "lock it"
|
|
||||||
while 1:
|
|
||||||
self.fd = None
|
|
||||||
try:
|
|
||||||
self.fd = os.open(self.lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR)
|
|
||||||
except OSError, e:
|
|
||||||
if e.errno != errno.EEXIST:
|
|
||||||
raise
|
|
||||||
if DBG: print "create error %s" % e.errno
|
|
||||||
except:
|
|
||||||
raise
|
|
||||||
if DBG: print "lock() - self.fd = '%s'" % self.fd
|
|
||||||
if not self.fd:
|
|
||||||
if self.oproc():
|
|
||||||
if DBG: print "process is alive"
|
|
||||||
os.remove(self.lock_file)
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
if DBG: print "no pid process??"
|
|
||||||
|
|
||||||
if self.fd:
|
|
||||||
f=os.fdopen(self.fd, 'w').write("%s" % os.getpid())
|
|
||||||
return self.fd
|
|
||||||
|
|
||||||
|
|
||||||
def unlock(self):
|
|
||||||
os.remove(self.lock_file)
|
|
||||||
self.fd=None
|
|
||||||
|
|
||||||
|
|
||||||
def setopid(self):
|
|
||||||
try:
|
|
||||||
self.opid=open(self.lock_file).readline()
|
|
||||||
if DBG: print "setopid = '%s'" % self.opid
|
|
||||||
if self.opid == '':
|
|
||||||
os.remove(self.lock_file)
|
|
||||||
except:
|
|
||||||
self.opid = ''
|
|
||||||
if self.opid == '':
|
|
||||||
self.opid = None
|
|
||||||
|
|
||||||
|
|
||||||
def oproc(self):
|
|
||||||
self.setopid()
|
|
||||||
if not self.opid:
|
|
||||||
return False
|
|
||||||
try:
|
|
||||||
os.kill(int(self.opid), 0)
|
|
||||||
return True
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
return False
|
|
||||||
|
|
||||||
def handler(signum, frame):
|
def handler(signum, frame):
|
||||||
global up
|
global up
|
||||||
if up == 0:
|
if up == 0:
|
||||||
@@ -117,6 +62,38 @@ def socksend(msg, tohost):
|
|||||||
sock.sendto(msg, tohost)
|
sock.sendto(msg, tohost)
|
||||||
if verbose: print "msg %s sent" % msg
|
if verbose: print "msg %s sent" % msg
|
||||||
|
|
||||||
|
def process():
|
||||||
|
ackcount=0
|
||||||
|
lastT=time.time()
|
||||||
|
while up:
|
||||||
|
sleep=(lastT+interval) - time.time()
|
||||||
|
if verbose: print "sleep %s" % sleep
|
||||||
|
if sleep > 0:
|
||||||
|
try:
|
||||||
|
r=select.select([sock.fileno()],[],[],sleep)
|
||||||
|
# time.sleep(interval)
|
||||||
|
except:
|
||||||
|
break
|
||||||
|
if verbose: print r
|
||||||
|
if sock.fileno() in r[0]:
|
||||||
|
data, addr = sock.recvfrom(1024)
|
||||||
|
if data == "ACK":
|
||||||
|
ackcount+=1
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
os.system(data)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
continue
|
||||||
|
lastT=time.time()
|
||||||
|
for hb_host in hb_hosts:
|
||||||
|
try:
|
||||||
|
msg="interval=%s;name=%s;time=%s;acks=%s" % (interval, iam, time.time(), ackcount)
|
||||||
|
if verbose: print "sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port)
|
||||||
|
socksend(msg, (hb_host, hb_port))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
msgonly=False
|
msgonly=False
|
||||||
helpflag=False
|
helpflag=False
|
||||||
@@ -259,61 +236,33 @@ if daemon:
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
os.close(0)
|
if not DBG:
|
||||||
os.close(1)
|
os.close(0)
|
||||||
os.close(2)
|
os.close(1)
|
||||||
sys.stdin.close()
|
os.close(2)
|
||||||
sys.stdout = NullDevice()
|
sys.stdin.close()
|
||||||
sys.stderr = NullDevice()
|
sys.stdout = NullDevice()
|
||||||
|
sys.stderr = NullDevice()
|
||||||
os.chdir("/")
|
os.chdir("/")
|
||||||
os.setsid()
|
os.setsid()
|
||||||
os.umask(0)
|
os.umask(0)
|
||||||
|
|
||||||
|
|
||||||
while True:
|
lock = FileLock('/tmp/hbc.pid')
|
||||||
lock=Flock('/tmp/hbc.pid')
|
lock.acquire()
|
||||||
if DBG: print "get lock"
|
|
||||||
if lock.lock():
|
|
||||||
if DBG: print "got lock"
|
|
||||||
break
|
|
||||||
if not lock.oproc():
|
|
||||||
sys.exit(1)
|
|
||||||
os.kill(lock.opid,15)
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
up=1
|
up=1
|
||||||
signal.signal(signal.SIGTERM, handler)
|
signal.signal(signal.SIGTERM, handler)
|
||||||
signal.signal(signal.SIGHUP, handler)
|
signal.signal(signal.SIGHUP, handler)
|
||||||
ackcount=0
|
|
||||||
lastT=time.time()
|
|
||||||
while up:
|
|
||||||
sleep=(lastT+interval) - time.time()
|
try:
|
||||||
if verbose: print "sleep %s" % sleep
|
process()
|
||||||
if sleep > 0:
|
except:
|
||||||
try:
|
data='hbc died:\n'+traceback.format_exc()
|
||||||
r=select.select([sock.fileno()],[],[],sleep)
|
open("/tmp/hbc.log","a").write(data)
|
||||||
# time.sleep(interval)
|
sys.exit(1)
|
||||||
except:
|
|
||||||
break
|
|
||||||
if verbose: print r
|
|
||||||
if sock.fileno() in r[0]:
|
|
||||||
data, addr = sock.recvfrom(1024)
|
|
||||||
if data == "ACK":
|
|
||||||
ackcount+=1
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
os.system(data)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
continue
|
|
||||||
lastT=time.time()
|
|
||||||
for hb_host in hb_hosts:
|
|
||||||
try:
|
|
||||||
msg="interval=%s;name=%s;time=%s;acks=%s" % (interval, iam, time.time(), ackcount)
|
|
||||||
if verbose: print "sock.send('%s', (%s, %s))" % (msg, hb_host, hb_port)
|
|
||||||
socksend(msg, (hb_host, hb_port))
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
up=0
|
up=0
|
||||||
msg="shutdown=1;name=%s;acks=%s" % (iam, ackcount)
|
msg="shutdown=1;name=%s;acks=%s" % (iam, ackcount)
|
||||||
@@ -322,4 +271,4 @@ for hb_host in hb_hosts:
|
|||||||
socksend(msg, (hb_host, hb_port))
|
socksend(msg, (hb_host, hb_port))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
sock.close()
|
sock.close()
|
||||||
lock.unlock()
|
lock.release()
|
||||||
|
|||||||
Reference in New Issue
Block a user