[object Object]

← back to Exo

changes to manual_discovery config based on PR comments

5e474135524b843037ae5c24d380661806a484f1 · 2024-10-22 13:00:46 +0700 · Ian Paul

Files touched

Diff

commit 5e474135524b843037ae5c24d380661806a484f1
Author: Ian Paul <iandouglaspaul@gmail.com>
Date:   Tue Oct 22 13:00:46 2024 +0700

    changes to manual_discovery config based on PR comments
---
 exo/networking/manual/manual_discovery.py          | 37 +++++++++++++---------
 exo/networking/manual/network_topology_config.py   |  1 -
 .../manual/test_network_topology_config.py         |  2 --
 3 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/exo/networking/manual/manual_discovery.py b/exo/networking/manual/manual_discovery.py
index e1fea5c6..10a8a6bd 100644
--- a/exo/networking/manual/manual_discovery.py
+++ b/exo/networking/manual/manual_discovery.py
@@ -51,24 +51,31 @@ class ManualDiscovery(Discovery):
         await asyncio.sleep(0.1)
     return list(self.known_peers.values())
 
+
   async def task_find_peers_from_config(self):
     if DEBUG_DISCOVERY >= 2: print("Starting task to find peers from config...")
     while True:
       for peer_id, peer_config in self.peers_in_network.items():
-        if DEBUG_DISCOVERY >= 2: print(f"Checking peer {peer_id=} at {peer_config.address}:{peer_config.port}")
-        peer = self.known_peers.get(peer_id)
-        if not peer:
-          if DEBUG_DISCOVERY >= 2: print(f"{peer_id=} not found in known peers. Adding.")
-          new_peer_handle = self.create_peer_handle(peer_id, f"{peer_config.address}:{peer_config.port}", peer_config.device_capabilities)
-          self.known_peers[peer_id] = new_peer_handle
-          peer = new_peer_handle
-
-        is_healthy = await peer.health_check()
-        if not is_healthy:
-          if DEBUG_DISCOVERY >= 2: print(f"{peer_id=} at {peer_config.address}:{peer_config.port} is not healthy. Removing.")
-          del self.known_peers[peer_id]
-        elif DEBUG_DISCOVERY >= 2: print(f"{peer_id=} at {peer_config.address}:{peer_config.port} is healthy.")
+        try:
+          if DEBUG_DISCOVERY >= 2: print(f"Checking peer {peer_id=} at {peer_config.address}:{peer_config.port}")
+          peer = self.known_peers.get(peer_id)
+          if not peer:
+            if DEBUG_DISCOVERY >= 2: print(f"{peer_id=} not found in known peers. Adding.")
+            peer = self.create_peer_handle(peer_id, f"{peer_config.address}:{peer_config.port}", peer_config.device_capabilities)  
+          is_healthy = await peer.health_check()
+          if is_healthy:
+            if DEBUG_DISCOVERY >= 2: print(f"{peer_id=} at {peer_config.address}:{peer_config.port} is healthy.")
+            self.known_peers[peer_id] = peer
+          else:
+            if DEBUG_DISCOVERY >= 2: print(f"{peer_id=} at {peer_config.address}:{peer_config.port} is not healthy.")
+            try:
+              del self.known_peers[peer_id]
+            except KeyError:
+              pass  # peer was never added, so nothing to delete
+        except Exception as e:
+            if DEBUG_DISCOVERY >=2: print(f"Exception occured when attempting to add {peer_id=}: {e}")
+        finally:
+          await asyncio.sleep(self.discovery_timeout)
 
-        if DEBUG_DISCOVERY >= 2: print(f"Current known peers: {[peer.id() for peer in self.known_peers.values()]}")
+      if DEBUG_DISCOVERY >= 2: print(f"Current known peers: {[peer.id() for peer in self.known_peers.values()]}")
 
-      await asyncio.sleep(self.discovery_timeout)
diff --git a/exo/networking/manual/network_topology_config.py b/exo/networking/manual/network_topology_config.py
index e8194d13..08cfa5f1 100644
--- a/exo/networking/manual/network_topology_config.py
+++ b/exo/networking/manual/network_topology_config.py
@@ -1,4 +1,3 @@
-import json
 from typing import Dict
 from pydantic import BaseModel, ValidationError
 
diff --git a/exo/networking/manual/test_network_topology_config.py b/exo/networking/manual/test_network_topology_config.py
index 8ddafde7..b5b2fbd5 100644
--- a/exo/networking/manual/test_network_topology_config.py
+++ b/exo/networking/manual/test_network_topology_config.py
@@ -1,7 +1,5 @@
 import unittest
-import json
 
-from pydantic import ValidationError
 from exo.networking.manual.network_topology_config import NetworkTopology
 
 root_path = "./exo/networking/manual/test_data/"

← 6b48a936 add pydantic dependency  ·  back to Exo  ·  Getting the setup script to work on Intel based Mac machines e1ae6d5a →