From 88d26241f5f3685e011b710bcc40bb2e609a32bb Mon Sep 17 00:00:00 2001 From: Hemna Date: Wed, 17 Apr 2024 12:24:56 -0400 Subject: [PATCH] Added try except blocks in collectors This patch adds some try except blocks in both the stats collector and the packets collector calls to registered objects. This can prevent the rest of APRSD falling down when the collector objects have a failure of some sort. --- aprsd/packets/collector.py | 15 +++++++++++++-- aprsd/stats/collector.py | 9 ++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/aprsd/packets/collector.py b/aprsd/packets/collector.py index d6c6659..2c9d334 100644 --- a/aprsd/packets/collector.py +++ b/aprsd/packets/collector.py @@ -1,9 +1,13 @@ +import logging from typing import Callable, Protocol, runtime_checkable from aprsd.packets import core from aprsd.utils import singleton +LOG = logging.getLogger("APRSD") + + @runtime_checkable class PacketMonitor(Protocol): """Protocol for Monitoring packets in some way.""" @@ -29,7 +33,11 @@ class PacketCollector: for name in self.monitors: cls = name() if isinstance(cls, PacketMonitor): - cls.rx(packet) + try: + cls.rx(packet) + except Exception as e: + LOG.error(f"Error in monitor {name} (rx): {e}") + else: raise TypeError(f"Monitor {name} is not a PacketMonitor") @@ -37,6 +45,9 @@ class PacketCollector: for name in self.monitors: cls = name() if isinstance(cls, PacketMonitor): - cls.tx(packet) + try: + cls.tx(packet) + except Exception as e: + LOG.error(f"Error in monitor {name} (tx): {e}") else: raise TypeError(f"Monitor {name} is not a PacketMonitor") diff --git a/aprsd/stats/collector.py b/aprsd/stats/collector.py index 3325669..91e1833 100644 --- a/aprsd/stats/collector.py +++ b/aprsd/stats/collector.py @@ -1,8 +1,12 @@ +import logging from typing import Callable, Protocol, runtime_checkable from aprsd.utils import singleton +LOG = logging.getLogger("APRSD") + + @runtime_checkable class StatsProducer(Protocol): """The StatsProducer protocol is used to define the interface for collecting stats.""" @@ -22,7 +26,10 @@ class Collector: for name in self.producers: cls = name() if isinstance(cls, StatsProducer): - stats[cls.__class__.__name__] = cls.stats(serializable=serializable) + try: + stats[cls.__class__.__name__] = cls.stats(serializable=serializable) + except Exception as e: + LOG.error(f"Error in producer {name} (stats): {e}") else: raise TypeError(f"{cls} is not an instance of StatsProducer") return stats