Commits

Anonymous committed cd86313

reconnect on failing read from websocket

Comments (0)

Files changed (1)

araldo_websocket/endpoints/websocket_endpoint.py

 from araldo.endpoints import EndPointBase
 
 
+RECONNECT_DELAY = 5
+
+
 class EndPoint(EndPointBase):  # pylint: disable-msg=R0904
     """ EndPoint implementation for connecting to a WebSocket server
     """
             :param gevent_queue: queue for forwarding incoming messages
         """
         config = kwargs["config"]
-        websocket_url = config["websocket_url"]
+        self._websocket_url = config["websocket_url"]
         do_trace = config["trace"]
         enableTrace(do_trace)
-        self._websocket = create_connection(websocket_url)
         EndPointBase.__init__(self, **kwargs)
+        self._connect_websocket()
+
+    def _connect_websocket(self):
+        try:
+            self._logger.debug(
+                "Connecting to WebSocket '%s'", self._websocket_url)
+            self._websocket = create_connection(self._websocket_url)
+        except Exception as error:
+            self._logger.debug(
+                "Reconnection attempt failed (%s); waiting & retrying...",
+                error)
+            gevent.sleep(RECONNECT_DELAY)
 
     @staticmethod
     def plugin_id():
                 self._logger.debug(
                     "Unable to unmarshal message; discarding (%s)",
                     error)
+                gevent.sleep(1)  # avoid tight loop
+
+                self._logger.debug("Trying to reconnect...")
+                self._connect_websocket()
+
             gevent.sleep(0)
 
     def check(self):