From 4476cedadde492436eaab30ac556098699567502 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Thu, 8 Dec 2022 11:50:24 +0200 Subject: [PATCH 1/4] Add a restart mechanism to all workers in the multiplexer --- sanic/worker/multiplexer.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sanic/worker/multiplexer.py b/sanic/worker/multiplexer.py index f92e7e08cc..0e26281135 100644 --- a/sanic/worker/multiplexer.py +++ b/sanic/worker/multiplexer.py @@ -21,9 +21,14 @@ def ack(self): "state": ProcessState.ACKED.name, } - def restart(self, name: str = ""): + def restart(self, name: str = "", all_workers: bool = False): + if name and all: + raise ValueError( + "Ambiguous restart with both a named process and" + " all_workers=True" + ) if not name: - name = self.name + name = "__ALL_PROCESSES__" if all_workers else self.name self._monitor_publisher.send(name) reload = restart # no cov From 763bf12e448c63346b7e1cc0c17e3d580d6f0f71 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Thu, 8 Dec 2022 11:51:47 +0200 Subject: [PATCH 2/4] Use consistent message as inspector --- sanic/worker/multiplexer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/worker/multiplexer.py b/sanic/worker/multiplexer.py index 0e26281135..836d6ac561 100644 --- a/sanic/worker/multiplexer.py +++ b/sanic/worker/multiplexer.py @@ -28,7 +28,7 @@ def restart(self, name: str = "", all_workers: bool = False): " all_workers=True" ) if not name: - name = "__ALL_PROCESSES__" if all_workers else self.name + name = "__ALL_PROCESSES__:" if all_workers else self.name self._monitor_publisher.send(name) reload = restart # no cov From d6097be7b050b30ee0814ad264ba9ea0c2706d61 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Thu, 8 Dec 2022 13:57:02 +0200 Subject: [PATCH 3/4] squash --- sanic/worker/multiplexer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/worker/multiplexer.py b/sanic/worker/multiplexer.py index 836d6ac561..91030fe628 100644 --- a/sanic/worker/multiplexer.py +++ b/sanic/worker/multiplexer.py @@ -22,7 +22,7 @@ def ack(self): } def restart(self, name: str = "", all_workers: bool = False): - if name and all: + if name and all_workers: raise ValueError( "Ambiguous restart with both a named process and" " all_workers=True" From 15ab51752f1b2a3923e7b27a9ee997b40b1ddbfd Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Sun, 11 Dec 2022 11:18:40 +0200 Subject: [PATCH 4/4] Add tests --- tests/worker/test_multiplexer.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/worker/test_multiplexer.py b/tests/worker/test_multiplexer.py index 338d329472..5047ef7731 100644 --- a/tests/worker/test_multiplexer.py +++ b/tests/worker/test_multiplexer.py @@ -1,6 +1,6 @@ from multiprocessing import Event from os import environ, getpid -from typing import Any, Dict +from typing import Any, Dict, Type, Union from unittest.mock import Mock import pytest @@ -117,3 +117,26 @@ def test_properties( assert m.workers == worker_state assert m.state == worker_state["Test"] assert isinstance(m.state, WorkerState) + + +@pytest.mark.parametrize( + "params,expected", + ( + ({}, "Test"), + ({"name": "foo"}, "foo"), + ({"all_workers": True}, "__ALL_PROCESSES__:"), + ({"name": "foo", "all_workers": True}, ValueError), + ), +) +def test_restart_params( + monitor_publisher: Mock, + m: WorkerMultiplexer, + params: Dict[str, Any], + expected: Union[str, Type[Exception]], +): + if isinstance(expected, str): + m.restart(**params) + monitor_publisher.send.assert_called_once_with(expected) + else: + with pytest.raises(expected): + m.restart(**params)