diff src/server/server.py @ 1008:1593e00078d2

server: reconnect SERVICE_PROFILE if it has been disconnected: if the profile is disconnected twice in a short time (currently 15 s), Libervia will not try to reconnect it and will log an error message instead.
author Goffi <goffi@goffi.org>
date Fri, 12 Jan 2018 18:03:34 +0100
parents d0b27d1e2d50
children 6b573b4b5bb0
line wrap: on
line diff
--- a/src/server/server.py	Fri Jan 12 15:42:58 2018 +0100
+++ b/src/server/server.py	Fri Jan 12 18:03:34 2018 +0100
@@ -49,6 +49,7 @@
 import uuid
 import urlparse
 import urllib
+import time
 from httplib import HTTPS_PORT
 import libervia
 from libervia.server import websockets
@@ -1148,6 +1149,7 @@
         web_resource.Resource.__init__(self)
         self.register = None
         self.sat_host = sat_host
+        self._last_service_prof_disconnect = time.time()
         self.signalDeferred = {} # dict of deferred (key: profile, value: Deferred)
                                  # which manages the long polling HTTP request with signals
         self.queue = {}
@@ -1245,8 +1247,26 @@
         #     self.sat_host._logged(profile, request)
 
     def disconnected(self, profile):
+        if profile == C.SERVICE_PROFILE:
+            # if service profile has been disconnected, we try to reconnect it
+            # if we can't we show error message
+            # and if we have 2 disconnection in a short time, we don't try to reconnect
+            # and display an error message
+            disconnect_delta = time.time() - self._last_service_prof_disconnect
+            if disconnect_delta < 15:
+                log.error(_(u'Service profile disconnected twice in a short time, please check connection'))
+            else:
+                log.info(_(u"Service profile has been disconnected, but we need it! Reconnecting it..."))
+                self.sat_host.bridge.connect(profile,
+                    self.sat_host.options['passphrase'],
+                    {},
+                    errback=lambda failure_: log.error(_(u"Can't reconnect service profile, please check connection: {reason}").format(reason=failure_))
+                    )
+            self._last_service_prof_disconnect = time.time()
+            return
+
         if not profile in self.sat_host.prof_connected:
-            log.error("'disconnected' signal received for a not connected profile")
+            log.info(_(u"'disconnected' signal received for a not connected profile ({profile})").format(profile=profile))
             return
         self.sat_host.prof_connected.remove(profile)
         if profile in self.signalDeferred: