From 7243c3c73d5f08e000a78f893578b8f0727e5e7e Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Thu, 20 Jan 2022 00:01:13 +0000 Subject: [PATCH] scripts: Re-implement list_supervisor_processes using API. --- scripts/lib/supervisor.py | 26 ++++++++++++++++++++++++++ scripts/lib/zulip_tools.py | 19 ------------------- scripts/restart-server | 2 +- scripts/stop-server | 2 +- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/scripts/lib/supervisor.py b/scripts/lib/supervisor.py index fda2f0a5ee..0e37f5d0cf 100644 --- a/scripts/lib/supervisor.py +++ b/scripts/lib/supervisor.py @@ -25,3 +25,29 @@ def rpc() -> client.ServerProxy: return client.ServerProxy( "http://localhost", transport=UnixStreamTransport("/var/run/supervisor.sock") ) + + +def list_supervisor_processes(*filter_names: str) -> List[str]: + results = [] + processes = rpc().supervisor.getAllProcessInfo() + assert isinstance(processes, list) + for process in processes: + if process["group"] != process["name"]: + name = f"{process['group']}:{process['name']}" + else: + name = process["name"] + + if filter_names: + match = False + for filter_name in filter_names: + if filter_name.endswith(":*") and name.startswith(filter_name[:-1]): + match = True + break + if name == filter_name: + match = True + break + if not match: + continue + + results.append(name) + return results diff --git a/scripts/lib/zulip_tools.py b/scripts/lib/zulip_tools.py index 838ec3c3db..2481cd77a4 100755 --- a/scripts/lib/zulip_tools.py +++ b/scripts/lib/zulip_tools.py @@ -617,25 +617,6 @@ def has_application_server(once: bool = False) -> bool: ) -def list_supervisor_processes(*args: str) -> List[str]: - worker_status = subprocess.run( - ["supervisorctl", "status", *args], - text=True, - stdout=subprocess.PIPE, - ) - # `supervisorctl status` returns 3 if any are stopped, which is - # fine here; and exit code 4 is for no such process, which is - # handled below. - if worker_status.returncode not in (0, 3, 4): - worker_status.check_returncode() - - processes = [] - for status_line in worker_status.stdout.splitlines(): - if not re.search(r"ERROR \(no such (process|group)\)", status_line): - processes.append(status_line.split()[0]) - return processes - - def has_process_fts_updates() -> bool: return ( # Current path diff --git a/scripts/restart-server b/scripts/restart-server index 6923988a66..06bbad7165 100755 --- a/scripts/restart-server +++ b/scripts/restart-server @@ -8,6 +8,7 @@ import sys import time sys.path.append(os.path.join(os.path.dirname(__file__), "..")) +from scripts.lib.supervisor import list_supervisor_processes from scripts.lib.zulip_tools import ( DEPLOYMENTS_DIR, ENDC, @@ -18,7 +19,6 @@ from scripts.lib.zulip_tools import ( get_tornado_ports, has_application_server, has_process_fts_updates, - list_supervisor_processes, overwrite_symlink, start_arg_parser, ) diff --git a/scripts/stop-server b/scripts/stop-server index 03ca9a1640..a1a6ea0af9 100755 --- a/scripts/stop-server +++ b/scripts/stop-server @@ -7,13 +7,13 @@ import sys import time sys.path.append(os.path.join(os.path.dirname(__file__), "..")) +from scripts.lib.supervisor import list_supervisor_processes from scripts.lib.zulip_tools import ( ENDC, OKGREEN, WARNING, has_application_server, has_process_fts_updates, - list_supervisor_processes, ) deploy_path = os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))