From cae900b1fb089ec99d79a0051ef974e4adc0208e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 31 Jul 2020 13:04:27 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20Try=20up=20to=20three=20times=20?= =?UTF-8?q?to=20check=20for=20connectivity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apparently some machines out there are a bit flaky after boot and it can take a bit for connectivity to work. As suggested by @fieldOfView in https://community.octoprint.org/t//177/9 we thus check connectivity three times in a row if negative before reporting it as negative. --- src/octoprint/util/connectivity.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/octoprint/util/connectivity.py b/src/octoprint/util/connectivity.py index 2cd90f964..93145f7d0 100644 --- a/src/octoprint/util/connectivity.py +++ b/src/octoprint/util/connectivity.py @@ -7,6 +7,7 @@ __copyright__ = "Copyright (C) 2020 The OctoPrint Project - Released under terms import threading import logging +import time from octoprint.util.net import server_reachable, resolve_host @@ -171,16 +172,26 @@ class ConnectivityChecker(object): self._logger.debug("Checking against {}:{} if we are online...".format(self._host, self._port)) old_value = self._online - self._connection_working = server_reachable(self._host, port=self._port) - if self._name: - if self._connection_working: - self._logger.debug("Checking if we can resolve {}...".format(self._name)) - self._resolution_working = len(resolve_host(self._name)) > 0 + for _ in range(3): + connection_working = server_reachable(self._host, port=self._port) + + if self._name: + if connection_working: + self._logger.debug("Checking if we can resolve {}...".format(self._name)) + resolution_working = len(resolve_host(self._name)) > 0 + else: + resolution_working = False else: - self._resolution_working = False - else: - self._resolution_working = True + resolution_working = True + + if not (connection_working and resolution_working): + # retry up to 3 times + time.sleep(1.0) + continue + + self._connection_working = connection_working + self._resolution_working = resolution_working if old_value != self._online: self._trigger_change(old_value, self._online)