Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration for running health checks without threads #362

Merged
merged 8 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions health_check/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
HEALTH_CHECK.setdefault("DISK_USAGE_MAX", 90)
HEALTH_CHECK.setdefault("MEMORY_MIN", 100)
HEALTH_CHECK.setdefault("WARNINGS_AS_ERRORS", True)
HEALTH_CHECK.setdefault("DISABLE_THREADING", False)
28 changes: 17 additions & 11 deletions health_check/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,22 @@ def _run(plugin):

connections.close_all()

with ThreadPoolExecutor(max_workers=len(self.plugins) or 1) as executor:
for plugin in executor.map(_run, self.plugins):
if plugin.critical_service:
if not HEALTH_CHECK["WARNINGS_AS_ERRORS"]:
errors.extend(
e
for e in plugin.errors
if not isinstance(e, ServiceWarning)
)
else:
errors.extend(plugin.errors)
def _collect_errors(plugin):
if plugin.critical_service:
if not HEALTH_CHECK["WARNINGS_AS_ERRORS"]:
errors.extend(
e for e in plugin.errors if not isinstance(e, ServiceWarning)
)
else:
errors.extend(plugin.errors)

if HEALTH_CHECK["DISABLE_THREADING"]:
for plugin in self.plugins:
_run(plugin)
_collect_errors(plugin)
else:
with ThreadPoolExecutor(max_workers=len(self.plugins) or 1) as executor:
for plugin in executor.map(_run, self.plugins):
_collect_errors(plugin)

return errors
40 changes: 37 additions & 3 deletions tests/test_mixins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from unittest.mock import patch

import pytest

from health_check.backends import BaseHealthCheckBackend
from health_check.conf import HEALTH_CHECK
from health_check.mixins import CheckMixin
from health_check.plugins import plugin_dir

Expand Down Expand Up @@ -28,11 +31,42 @@ def setup(self):
yield
plugin_dir.reset()

def test_plugins(self):
@pytest.mark.parametrize("disable_threading", [(True,), (False,)])
def test_plugins(self, monkeypatch, disable_threading):
monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading)

assert len(Checker().plugins) == 2

def test_errors(self):
@pytest.mark.parametrize("disable_threading", [(True,), (False,)])
def test_errors(self, monkeypatch, disable_threading):
monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading)

assert len(Checker().errors) == 1

def test_run_check(self):
@pytest.mark.parametrize("disable_threading", [(True,), (False,)])
def test_run_check(self, monkeypatch, disable_threading):
monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading)

assert len(Checker().run_check()) == 1

def test_run_check_threading_enabled(self, monkeypatch):
"""Ensure threading used when not disabled."""

# Ensure threading is enabled.
monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", False)

# Ensure ThreadPoolExecutor is used
with patch("health_check.mixins.ThreadPoolExecutor") as tpe:
Checker().run_check()
tpe.assert_called()

def test_run_check_threading_disabled(self, monkeypatch):
"""Ensure threading not used when disabled."""

# Ensure threading is disabled.
monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", True)

# Ensure ThreadPoolExecutor is not used
with patch("health_check.mixins.ThreadPoolExecutor") as tpe:
Checker().run_check()
tpe.assert_not_called()