def __init__( self, oef_addr: str, oef_port: int, loop: Optional[asyncio.AbstractEventLoop] = None, ): """ Initialize. :param oef_addr: IP address of the OEF node. :param oef_port: Port of the OEF node. """ self.oef_addr = oef_addr self.oef_port = oef_port self._result = False self._stop = False self._core = AsyncioCore() self.agent = OEFAgent("check", core=self._core, oef_addr=self.oef_addr, oef_port=self.oef_port) self.agent.on_connect_success = self.on_connect_ok self.agent.on_connection_terminated = self.on_connect_terminated self.agent.on_connect_failed = self.exception_handler
class OEFHealthCheck(object): """A health check class.""" def __init__( self, oef_addr: str, oef_port: int, loop: Optional[asyncio.AbstractEventLoop] = None, ): """ Initialize. :param oef_addr: IP address of the OEF node. :param oef_port: Port of the OEF node. """ self.oef_addr = oef_addr self.oef_port = oef_port self._result = False self._stop = False self._core = AsyncioCore() self.agent = OEFAgent("check", core=self._core, oef_addr=self.oef_addr, oef_port=self.oef_port) self.agent.on_connect_success = self.on_connect_ok self.agent.on_connection_terminated = self.on_connect_terminated self.agent.on_connect_failed = self.exception_handler def exception_handler(self, url=None, ex=None): """Handle exception during a connection attempt.""" print("An error occurred. Exception: {}".format(ex)) self._stop = True def on_connect_ok(self, url=None): """Handle a successful connection.""" print("Connection OK!") self._result = True self._stop = True def on_connect_terminated(self, url=None): """Handle a connection failure.""" print("Connection terminated.") self._stop = True def run(self) -> bool: """ Run the check, asynchronously. :return: True if the check is successful, False otherwise. """ self._result = False self._stop = False def stop_connection_attempt(self): if self.agent.state == "connecting": self.agent.state = "failed" t = Timer(1.5, stop_connection_attempt, args=(self, )) try: print("Connecting to {}:{}...".format(self.oef_addr, self.oef_port)) self._core.run_threaded() t.start() self._result = self.agent.connect() self._stop = True if self._result: print("Connection established. Tearing down connection...") self.agent.disconnect() t.cancel() else: print("A problem occurred. Exiting...") return self._result except Exception as e: print(str(e)) return self._result finally: t.join(1.0) self.agent.stop() self.agent.disconnect() self._core.stop()