Browse files

add option to cli tools to always reconnect to broker on error

  • Loading branch information...
1 parent e5d1a21 commit 30914f76042a15911b51884c275a9bb09c3568ee @ArthurClune ArthurClune committed May 15, 2012
Showing with 22 additions and 6 deletions.
  1. +22 −6 cli/hpfeeds.py
View
28 cli/hpfeeds.py
@@ -3,6 +3,7 @@
import socket
import hashlib
import logging
+from time import sleep
logger = logging.getLogger('pyhpfeeds')
@@ -52,10 +53,12 @@ class FeedException(Exception):
pass
class HPC(object):
- def __init__(self, host, port, ident, secret, timeout=3):
+ def __init__(self, host, port, ident, secret, timeout=3, reconnect=False, sleepwait=20):
self.host, self.port = host, port
self.ident, self.secret = ident, secret
self.timeout = timeout
+ self.reconnect = reconnect
+ self.sleepwait = sleepwait
self.brokername = 'unknown'
self.connected = False
self.stopped = False
@@ -91,7 +94,7 @@ def connect(self):
self.s.settimeout(None)
- def run(self, message_callback, error_callback):
+ def _run(self, message_callback, error_callback):
while not self.stopped:
while self.connected:
d = self.s.recv(BUFSIZ)
@@ -115,6 +118,21 @@ def run(self, message_callback, error_callback):
if self.stopped: break
self.connect()
+ def run(self, message_callback, error_callback):
+ if not self.reconnect:
+ self._run(message_callback, error_callback)
+ else:
+ while True:
+ self._run(message_callback, error_callback)
+ # reconnect now we've failed
+ sleep(self.sleepwait)
+ while True:
+ try:
+ self.connect()
+ break
+ except FeedException:
+ sleep(self.sleepwait)
+
def subscribe(self, chaninfo):
if type(chaninfo) == str:
chaninfo = [chaninfo,]
@@ -133,8 +151,6 @@ def close(self):
try: self.s.close()
except: logger.warn('Socket exception when closing.')
-def new(host=None, port=10000, ident=None, secret=None):
- if not host or not ident or not secret:
- raise Exception('host, ident and secret are mandatory!')
- return HPC(host, port, ident, secret)
+def new(host=None, port=10000, ident=None, secret=None, reconnect=True, sleepwait=20):
+ return HPC(host, port, ident, secret, reconnect)

0 comments on commit 30914f7

Please sign in to comment.