From 9b2e4f9d0c76d24b8f6fcce0e76a58ca64e53e40 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 17 Feb 2020 13:39:40 +0100 Subject: [PATCH] Added count parameter to the ping backend --- platypush/backend/ping.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/platypush/backend/ping.py b/platypush/backend/ping.py index 321d44d1..15dc321a 100644 --- a/platypush/backend/ping.py +++ b/platypush/backend/ping.py @@ -22,31 +22,35 @@ class PingBackend(Backend): class Pinger(Worker): def __init__(self, *args, **kwargs): self.timeout = kwargs.pop('timeout') + self.count = kwargs.pop('count') super().__init__(*args, **kwargs) def process(self, host: str) -> Tuple[str, bool]: pinger = get_plugin('ping') - response = pinger.ping(host, timeout=self.timeout, count=1).output + response = pinger.ping(host, timeout=self.timeout, count=self.count).output return host, response['success'] is True - def __init__(self, hosts: List[str], timeout: float = 5.0, interval: float = 60.0, *args, **kwargs): + def __init__(self, hosts: List[str], timeout: float = 5.0, interval: float = 60.0, count: int = 1, *args, **kwargs): """ :param hosts: List of IP addresses or host names to monitor. :param timeout: Ping timeout. :param interval: Interval between two scans. + :param count: Number of pings per host. A host will be considered down + if all the ping requests fail. """ super().__init__(*args, **kwargs) self.hosts = {h: None for h in hosts} self.timeout = timeout self.interval = interval + self.count = count def run(self): super().run() self.logger.info('Starting ping backend with {} hosts to monitor'.format(len(self.hosts))) while not self.should_stop(): - workers = Workers(10, self.Pinger, timeout=self.timeout) + workers = Workers(10, self.Pinger, timeout=self.timeout, count=self.count) with workers: for host in self.hosts.keys():