Skip to content

Commit

Permalink
Log exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikmakait committed Aug 1, 2024
1 parent 3d47d3a commit 539969b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
31 changes: 30 additions & 1 deletion distributed/diagnostics/tests/test_nanny_plugin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

import logging

import pytest

from distributed import Nanny, NannyPlugin
from distributed.protocol.pickle import dumps
from distributed.utils_test import gen_cluster
from distributed.utils_test import captured_logger, gen_cluster


@gen_cluster(client=True, nthreads=[("", 1)], Worker=Nanny)
Expand Down Expand Up @@ -160,3 +162,30 @@ def setup(self, nanny):
await c.register_plugin(second, idempotent=True)
assert "idempotentplugin" in a.plugins
assert a.plugins["idempotentplugin"].instance == "first"


class BrokenSetupPlugin(NannyPlugin):
def setup(self, nanny):
raise RuntimeError("test error")


@gen_cluster(client=True, nthreads=[("", 1)], Worker=Nanny)
async def test_register_plugin_with_broken_setup_to_existing_nanny_raises(c, s, a):
with pytest.raises(RuntimeError, match="test error"):
with captured_logger("distributed.nanny", level=logging.ERROR) as caplog:
await c.register_plugin(BrokenSetupPlugin(), name="TestPlugin1")
logs = caplog.getvalue()
assert "TestPlugin1 failed to setup" in logs
assert "test error" in logs


@gen_cluster(client=True, nthreads=[])
async def test_plugin_with_broken_setup_on_new_nanny_logs(c, s):
await c.register_plugin(BrokenSetupPlugin(), name="TestPlugin1")

with captured_logger("distributed.nanny", level=logging.ERROR) as caplog:
async with Nanny(s.address):
pass
logs = caplog.getvalue()
assert "TestPlugin1 failed to setup" in logs
assert "test error" in logs
3 changes: 2 additions & 1 deletion distributed/nanny.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,14 @@ async def plugin_add(

self.plugins[name] = plugin

logger.info("Starting Nanny plugin %s" % name)
logger.info("Starting Nanny plugin %s", name)
if hasattr(plugin, "setup"):
try:
result = plugin.setup(nanny=self)
if isawaitable(result):
result = await result
except Exception as e:
logger.exception("Worker plugin %s failed to setup", name)
return error_message(e)
if getattr(plugin, "restart", False):
await self.restart(reason=f"nanny-plugin-{name}-restart")
Expand Down

0 comments on commit 539969b

Please sign in to comment.