From 22b266e2615e2e6af882c83b49a56ef25f54fea5 Mon Sep 17 00:00:00 2001 From: ATATC Date: Thu, 2 May 2024 10:44:36 -0400 Subject: [PATCH 1/3] Supported auto-retry on disconnection or failure. (#121) --- leads_vec_rc/cli.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/leads_vec_rc/cli.py b/leads_vec_rc/cli.py index 7a4546de..cc1b1f26 100644 --- a/leads_vec_rc/cli.py +++ b/leads_vec_rc/cli.py @@ -3,6 +3,7 @@ from json import loads, JSONDecodeError from os import mkdir from os.path import abspath, exists +from time import sleep from typing import Any from fastapi import FastAPI @@ -29,14 +30,27 @@ ), time_stamp_record, voltage_record, speed_record, None, None, None, None, None, None, None, None, None) +def retry(service: Service) -> Client: + L.warn("Retrying connection...") + return start_client(config.comm_addr, create_client(service.port(), callback), True) + + class CommCallback(Callback): + def __init__(self) -> None: + super().__init__() + self.client: Client = start_client(config.comm_addr, create_client(config.comm_port, self), True) + def on_connect(self, service: Service, connection: Connection) -> None: self.super(service=service, connection=connection) - L.debug("Connected") + L.info("Connected") def on_fail(self, service: Service, error: Exception) -> None: self.super(service=service, error=error) L.error(f"Comm client error: {repr(error)}") + if isinstance(error, ConnectionRefusedError): + sleep(3) + assert isinstance(service, Client) + self.client = retry(service) def on_receive(self, service: Service, msg: bytes) -> None: self.super(service=service, msg=msg) @@ -54,10 +68,17 @@ def on_receive(self, service: Service, msg: bytes) -> None: except JSONDecodeError as e: L.error(repr(e)) + def on_disconnect(self, service: Service, connection: ConnectionBase) -> None: + self.super(service=service, connection=connection) + L.info("Disconnected") + sleep(3) + assert isinstance(service, Client) + self.client = retry(service) + -client = start_client(config.comm_addr, create_client(config.comm_port, CommCallback()), True) +callback: CommCallback = CommCallback() -app = FastAPI(title="LEADS VeC Remote Analyst") +app: FastAPI = FastAPI(title="LEADS VeC Remote Analyst") app.add_middleware( CORSMiddleware, @@ -90,13 +111,13 @@ async def speed() -> list[float]: @app.get("/time_lap") async def time_lap() -> str: - client.send(b"time_lap") + callback.client.send(b"time_lap") return "done" @app.get("/hazard") async def hazard() -> str: - client.send(b"hazard") + callback.client.send(b"hazard") return "done" From 36858ecca66699f8660f90ba1f4797578adcc1d3 Mon Sep 17 00:00:00 2001 From: ATATC Date: Thu, 2 May 2024 10:50:31 -0400 Subject: [PATCH 2/3] Increased waiting time. (#121) --- leads_vec_rc/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/leads_vec_rc/cli.py b/leads_vec_rc/cli.py index cc1b1f26..0c11ae8f 100644 --- a/leads_vec_rc/cli.py +++ b/leads_vec_rc/cli.py @@ -48,7 +48,7 @@ def on_fail(self, service: Service, error: Exception) -> None: self.super(service=service, error=error) L.error(f"Comm client error: {repr(error)}") if isinstance(error, ConnectionRefusedError): - sleep(3) + sleep(10) assert isinstance(service, Client) self.client = retry(service) @@ -71,7 +71,7 @@ def on_receive(self, service: Service, msg: bytes) -> None: def on_disconnect(self, service: Service, connection: ConnectionBase) -> None: self.super(service=service, connection=connection) L.info("Disconnected") - sleep(3) + sleep(10) assert isinstance(service, Client) self.client = retry(service) From e6457fdefd119acd6c4d60262d24204c7515ba36 Mon Sep 17 00:00:00 2001 From: ATATC Date: Thu, 2 May 2024 10:51:35 -0400 Subject: [PATCH 3/3] Removed an unnecessary conditional statement. (#121) --- leads_vec_rc/cli.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/leads_vec_rc/cli.py b/leads_vec_rc/cli.py index 0c11ae8f..c7c0601f 100644 --- a/leads_vec_rc/cli.py +++ b/leads_vec_rc/cli.py @@ -47,10 +47,9 @@ def on_connect(self, service: Service, connection: Connection) -> None: def on_fail(self, service: Service, error: Exception) -> None: self.super(service=service, error=error) L.error(f"Comm client error: {repr(error)}") - if isinstance(error, ConnectionRefusedError): - sleep(10) - assert isinstance(service, Client) - self.client = retry(service) + sleep(10) + assert isinstance(service, Client) + self.client = retry(service) def on_receive(self, service: Service, msg: bytes) -> None: self.super(service=service, msg=msg)