Skip to content

Commit d91f588

Browse files
committed
✨ Add timeout after which check is considered inactive
We don't want our firmware checks to stay endlessly active and get rechecked, usually things should be clear after the first few seconds of connection to a printer. In case this changes at some point we can override the timeout for individual checks or even outright disable it.
1 parent c4e4781 commit d91f588

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

octoprint_firmware_check/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ def _run_checks(self, check_type, *args, **kwargs):
176176
changes = True
177177
break
178178

179+
check.evaluate_timeout()
180+
self._logger.debug("Check {} active? {}".format(check, check.active))
181+
179182
if changes:
180183
self._ping_clients()
181184

octoprint_firmware_check/checks/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
55
__copyright__ = "Copyright (C) 2019 The OctoPrint Project - Released under terms of the AGPLv3 License"
66

7+
from octoprint.util import monotonic_time
8+
import logging
79

810
class Check(object):
911
name = None
1012
url = None
1113

14+
ACTIVE_TIMEOUT = 30.0
15+
1216
def __init__(self):
1317
self._active = True
1418
self._triggered = False
19+
self._start_time = None
1520

1621
def received(self, line):
1722
"""Called when receiving a new line from the printer"""
@@ -35,9 +40,20 @@ def triggered(self):
3540
"""Whether the check has been triggered"""
3641
return self._triggered
3742

43+
def evaluate_timeout(self):
44+
if self._start_time is None:
45+
self._start_time = monotonic_time()
46+
else:
47+
if monotonic_time() > self._start_time + self.ACTIVE_TIMEOUT:
48+
# timeout ran out, this is now inactive
49+
self._active = False
50+
logging.getLogger(__name__).info("Deactivated {} due to timeout after {}s".format(__name__, self.ACTIVE_TIMEOUT))
51+
3852
def reset(self):
3953
self._active = True
4054
self._triggered = False
55+
self._start_time = None
56+
4157

4258

4359
class AuthorCheck(Check):

0 commit comments

Comments
 (0)