From 0d40773c6e9d81c05adcba5fadb89c235fa4f133 Mon Sep 17 00:00:00 2001 From: Ouziel Slama Date: Tue, 8 Oct 2024 11:13:57 +0000 Subject: [PATCH] clean gunicorn workers after regtest --- .../counterpartycore/lib/api/wsgi.py | 2 +- .../test/regtest/regtestnode.py | 32 +++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/counterparty-core/counterpartycore/lib/api/wsgi.py b/counterparty-core/counterpartycore/lib/api/wsgi.py index 70b559202..0809f943e 100644 --- a/counterparty-core/counterpartycore/lib/api/wsgi.py +++ b/counterparty-core/counterpartycore/lib/api/wsgi.py @@ -162,7 +162,7 @@ def spawn_worker(self): worker.pid = os.getpid() try: gunicorn_util._setproctitle("worker [%s]" % self.proc_name) - self.log.info("Booting worker with pid: %s", worker.pid) + logger.debug("Booting Gunicorn worker with pid: %s", worker.pid) self.add_worker_to_pid_file(worker.pid) self.cfg.post_fork(self, worker) worker.init_process() diff --git a/counterparty-core/counterpartycore/test/regtest/regtestnode.py b/counterparty-core/counterpartycore/test/regtest/regtestnode.py index 72e037270..2e628c890 100644 --- a/counterparty-core/counterpartycore/test/regtest/regtestnode.py +++ b/counterparty-core/counterpartycore/test/regtest/regtestnode.py @@ -2,6 +2,7 @@ import json import os +import signal import sys import threading import time @@ -239,6 +240,7 @@ def start(self): "--regtest", f"--database-file={self.datadir}/counterparty.db", "--wsgi-server=gunicorn", + "--gunicorn-workers=1", "-vv", "start", _bg=True, @@ -266,6 +268,29 @@ def start(self): while True: time.sleep(1) + def get_gunicorn_workers_pids(self): + logs = self.server_out.getvalue() + pids = [] + for line in logs.splitlines(): + if "Booting Gunicorn worker with pid: " in line: + pid = int(line.split("Booting Gunicorn worker with pid: ")[1].split(" ")[0]) + pids.append(pid) + return pids + + def kill_gunicorn_workers(self): + pids = self.get_gunicorn_workers_pids() + print(f"Killing Gunicorn workers: {pids}") + for pid in pids: + try: + os.kill(pid, signal.SIGKILL) + except ProcessLookupError: + pass + + def stop_counterparty_server(self): + self.counterparty_server_process.terminate() + self.counterparty_server_process.wait() + self.kill_gunicorn_workers() + def stop(self): print("Stopping...") try: @@ -278,7 +303,7 @@ def stop(self): print(e) pass try: - self.counterparty_server_process.terminate() # noqa + self.stop_counterparty_server() # noqa except Exception as e: print(e) pass @@ -295,6 +320,7 @@ def check_node_state(self, previous_state): "--regtest", f"--database-file={self.datadir}/counterparty.db", "--wsgi-server=gunicorn", + "--gunicorn-workers=1", "-vv", "start", _bg=True, @@ -311,13 +337,13 @@ def check_node_state(self, previous_state): def test_command(self, command): state_before = self.get_node_state() - self.counterparty_server_process.terminate() # noqa - self.counterparty_server_process.wait() + self.stop_counterparty_server() print(f"Running `{command}`...") sh.counterparty_server( "--regtest", f"--database-file={self.datadir}/counterparty.db", "--wsgi-server=gunicorn", + "--gunicorn-workers=1", "-vv", command, 0,