Zeroconf reports system properties (#59)

* Zeroconf.found_clients() now returns dicts of clients, not just hostnames

* Adjustments to distributed_job_manager.py

* Undo config change

* Report system metrics (cpu, os, etc) via zeroconf_server.py

* Zeroconf.found_clients() now returns dicts of clients, not just hostnames

* Adjustments to distributed_job_manager.py

* Undo config change

* Zeroconf.found_clients() now returns dicts of clients, not just hostnames

* Adjustments to distributed_job_manager.py

* Undo config change

* Adjustments to distributed_job_manager.py

* Undo config change

* Rename ZeroconfServer.found_clients() to found_hostnames()
This commit is contained in:
2023-11-04 20:46:27 -05:00
committed by GitHub
parent d3b84c6212
commit 06a613fcc4
9 changed files with 38 additions and 33 deletions

View File

@@ -1,6 +1,7 @@
import logging
import socket
import zeroconf
from zeroconf import Zeroconf, ServiceInfo, ServiceBrowser, ServiceStateChange
logger = logging.getLogger()
@@ -24,6 +25,8 @@ class ZeroconfServer:
@classmethod
def start(cls, listen_only=False):
if not cls.service_type:
raise RuntimeError("The 'configure' method must be run before starting the zeroconf server")
if not listen_only:
cls._register_service()
cls._browse_services()
@@ -49,8 +52,8 @@ class ZeroconfServer:
cls.service_info = info
cls.zeroconf.register_service(info)
logger.info(f"Registered zeroconf service: {cls.service_info.name}")
except socket.gaierror as e:
logger.error(f"Error starting zeroconf service: {e}")
except zeroconf.NonUniqueNameException as e:
logger.error(f"Error establishing zeroconf: {e}")
@classmethod
def _unregister_service(cls):
@@ -75,19 +78,24 @@ class ZeroconfServer:
cls.client_cache.pop(name)
@classmethod
def found_clients(cls):
def found_hostnames(cls):
fetched_hostnames = [x.split(f'.{cls.service_type}')[0] for x in cls.client_cache.keys()]
local_hostname = socket.gethostname()
# Define a sort key function
def sort_key(hostname):
# Return 0 if it's the local hostname so it comes first, else return 1
return 0 if hostname == local_hostname else 1
return False if hostname == local_hostname else True
# Sort the list with the local hostname first
sorted_hostnames = sorted(fetched_hostnames, key=sort_key)
return sorted_hostnames
@classmethod
def get_hostname_properties(cls, hostname):
new_key = hostname + '.' + cls.service_type
server_info = cls.client_cache.get(new_key).properties
decoded_server_info = {key.decode('utf-8'): value.decode('utf-8') for key, value in server_info.items()}
return decoded_server_info
# Example usage:
if __name__ == "__main__":