Skip to content

Commit

Permalink
clean gunicorn workers after regtest
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Oct 8, 2024
1 parent 13c0543 commit 0d40773
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/api/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
32 changes: 29 additions & 3 deletions counterparty-core/counterpartycore/test/regtest/regtestnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import os
import signal
import sys
import threading
import time
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 0d40773

Please sign in to comment.