changeset 1367:ce5cfe004d4e

server: new `--bridge-retries` option: If bridge connection fails, it will be retried for the specified number of time (default to 10) before giving up. Each try is spaced by 1 second.
author Goffi <goffi@goffi.org>
date Fri, 20 Nov 2020 18:02:43 +0100
parents f61e95438050
children 9624c2a89a92
files libervia/server/server.py twisted/plugins/libervia_server.py
diffstat 2 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/server/server.py	Fri Nov 20 17:55:21 2020 +0100
+++ b/libervia/server/server.py	Fri Nov 20 18:02:43 2020 +0100
@@ -834,6 +834,7 @@
         self.ns_map = {}  # map of short name to namespaces
 
         ## bridge ##
+        self._bridge_retry = self.options['bridge-retries']
         self.bridge = Bridge()
         self.bridge.bridgeConnect(callback=self._bridgeCb, errback=self._bridgeEb)
 
@@ -1207,6 +1208,7 @@
         self.site.sessionFactory = LiberviaSession
 
     def _bridgeCb(self):
+        del self._bridge_retry
         self.bridge.getReady(
             lambda: self.initialised.callback(None),
             lambda failure: self.initialised.errback(Exception(failure)),
@@ -1215,6 +1217,22 @@
 
     def _bridgeEb(self, failure_):
         if isinstance(failure_, BridgeExceptionNoService):
+            if self._bridge_retry:
+                if self._bridge_retry < 0:
+                    print(_("Can't connect to bridge, will retry indefinitely. "
+                            "Next try in 1s."))
+                else:
+                    self._bridge_retry -= 1
+                    print(
+                        _(
+                            "Can't connect to bridge, will retry in 1 s ({retries_left} "
+                            "trie(s) left)."
+                        ).format(retries_left=self._bridge_retry)
+                    )
+                time.sleep(1)
+                self.bridge.bridgeConnect(callback=self._bridgeCb, errback=self._bridgeEb)
+                return
+
             print("Can't connect to SàT backend, are you sure it's launched ?")
         else:
             log.error("Can't connect to bridge: {}".format(failure))
--- a/twisted/plugins/libervia_server.py	Fri Nov 20 17:55:21 2020 +0100
+++ b/twisted/plugins/libervia_server.py	Fri Nov 20 18:02:43 2020 +0100
@@ -131,6 +131,8 @@
                        ['base_url_ext', '', '',
                         _('The external URL to use as base URL'),
                         coerceUnicode],
+                       ['bridge-retries', '', 10,
+                        _('Number of tries to connect to bridge before giving up'), int],
                       ]
 
 # Options which are in sat.conf only