summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/backend/backend.py
diff options
context:
space:
mode:
authorTomás Touceda <chiiph@leap.se>2014-07-24 15:26:47 -0300
committerTomás Touceda <chiiph@leap.se>2014-07-24 15:26:47 -0300
commitc01e87456bc5fded14778251bbd5dfe4fc4ebbd0 (patch)
tree38e48db1889e1af3b7b60cdb43ab7786798d557a /src/leap/bitmask/backend/backend.py
parentfaadf8dc3bb1e49adc23c64d5a851b7eea0ce2ee (diff)
parentce88b52295881a3dd1bc0efec8e72b1d3a235e87 (diff)
Merge remote-tracking branch 'refs/remotes/ivan/feature/add-frontend-alive-check' into develop
Diffstat (limited to 'src/leap/bitmask/backend/backend.py')
-rw-r--r--src/leap/bitmask/backend/backend.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/leap/bitmask/backend/backend.py b/src/leap/bitmask/backend/backend.py
index c895f8f5..67ffe35a 100644
--- a/src/leap/bitmask/backend/backend.py
+++ b/src/leap/bitmask/backend/backend.py
@@ -18,6 +18,8 @@ import json
import threading
import time
+import psutil
+
from twisted.internet import defer, reactor, threads
import zmq
@@ -39,12 +41,16 @@ class Backend(object):
PORT = '5556'
BIND_ADDR = "tcp://127.0.0.1:%s" % PORT
- def __init__(self):
+ PING_INTERVAL = 2 # secs
+
+ def __init__(self, frontend_pid=None):
"""
Backend constructor, create needed instances.
"""
self._signaler = Signaler()
+ self._frontend_pid = frontend_pid
+
self._do_work = threading.Event() # used to stop the worker thread.
self._zmq_socket = None
@@ -81,6 +87,8 @@ class Backend(object):
Note: we use a simple while since is less resource consuming than a
Twisted's LoopingCall.
"""
+ pid = self._frontend_pid
+ check_wait = 0
while self._do_work.is_set():
# Wait for next request from client
try:
@@ -93,6 +101,20 @@ class Backend(object):
raise
time.sleep(0.01)
+ check_wait += 0.01
+ if pid is not None and check_wait > self.PING_INTERVAL:
+ check_wait = 0
+ self._check_frontend_alive()
+
+ def _check_frontend_alive(self):
+ """
+ Check if the frontend is alive and stop the backend if it is not.
+ """
+ pid = self._frontend_pid
+ if pid is not None and not psutil.pid_exists(pid):
+ logger.critical("The frontend is down!")
+ self.stop()
+
def _stop_reactor(self):
"""
Stop the Twisted reactor, but first wait a little for some threads to