Commits

Josh VanderLinden committed 0203ef1

Trying to reconnect

  • Participants
  • Parent commits 9665bbb

Comments (0)

Files changed (2)

 # Maintainer: Josh VanderLinden <arch@cloudlery.com>
 pkgname=zedify
-pkgver=20130516
-pkgrel=5
+pkgver=20130519
+pkgrel=1
 pkgdesc="Deliver message notifications to remote machines"
 arch=('any')
 url="http://bitbucket.org/codekoala/zedify"

File zedify/i3.py

 SHORT_RE = re.compile(r'(\w.*?)[A-Z_]')
 SND_PATH = os.path.join(site.getsitepackages()[0], 'zedify',
                         'resources', 'receive.wav')
-REFRESH_INTERVAL = int(config.zedifier.get('interval', 10))
+REFRESH_INTERVAL = max(10, int(config.zedifier.get('interval', 10)))
 log = get_logger('zedifier')
 
 
-ctx = zmq.Context()
-
-SUB = ctx.socket(zmq.SUB)
-SUB.setsockopt_unicode(zmq.SUBSCRIBE, '')
-SUB.connect(config.zedifier.sub)
-
-PUSH = ctx.socket(zmq.PUSH)
-PUSH.connect(config.zedifier.push)
-
 MESSAGES = {}
 
 
         worker = None
         event = Event()
 
+        self.sub = None
+        self.push = None
+        self.ready = False
+
+        def connect():
+            ctx = zmq.Context()
+
+            self.sub = ctx.socket(zmq.SUB)
+            self.sub.setsockopt_unicode(zmq.SUBSCRIBE, '')
+            self.sub.connect(config.zedifier.sub)
+
+            self.push = ctx.socket(zmq.PUSH)
+            self.push.connect(config.zedifier.push)
+
+            time.sleep(2)
+
         def handle(msg):
             global MESSAGES
 
 
             last_update = time.time()
             p = zmq.Poller()
-            p.register(SUB, zmq.POLLIN)
+            p.register(self.sub, zmq.POLLIN)
 
             request_status(True)
 
                 while not ev.is_set():
                     # request a status update if we haven't received anything
                     # recently
-                    if time.time() - last_update >= REFRESH_INTERVAL:
+                    span = time.time() - last_update
+                    if span >= REFRESH_INTERVAL * 4:
+                        connect()
+
+                    if span >= REFRESH_INTERVAL:
                         request_status()
 
                     try:
                         socks = dict(p.poll(1000))
-                        if SUB in socks:
+                        if self.sub in socks:
                             last_update = time.time()
-                            handle(SUB.recv_json())
+                            handle(self.sub.recv_json())
                     except:
                         log.exception('Error')
 
             finally:
-                SUB.close()
+                self.sub.close()
 
         def request_status(full=False):
             log.info('Requesting unseen count update')
-            PUSH.send_json({'event': 'status', 'full': full})
+            self.push.send_json({'event': 'status', 'full': full})
 
         def stop_receiving(signal=None, frame=None):
             log.warn('Attempting to terminate zedifier')
 
             log.info('Done.')
 
+        connect()
+
         atexit.register(stop_receiving)
         worker = Thread(target=receive, args=[event])
         worker.start()