Changeset View
Changeset View
Standalone View
Standalone View
swh/graph/grpc_server.py
- This file was moved from swh/graph/rpc_server.py.
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 2021-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
""" | """ | ||||
A simple tool to start the swh-graph GRPC server in Java. | A simple tool to start the swh-graph GRPC server in Java. | ||||
""" | """ | ||||
import logging | import logging | ||||
import shlex | import shlex | ||||
import subprocess | import subprocess | ||||
import aiohttp.test_utils | import aiohttp.test_utils | ||||
import aiohttp.web | import aiohttp.web | ||||
from swh.graph.config import check_config | from swh.graph.config import check_config | ||||
def spawn_java_rpc_server(config, port=None): | def spawn_java_grpc_server(**config): | ||||
port = config.pop("port", None) | |||||
if port is None: | if port is None: | ||||
port = aiohttp.test_utils.unused_port() | port = aiohttp.test_utils.unused_port() | ||||
config = check_config(config or {}) | config = check_config(config) | ||||
cmd = [ | cmd = [ | ||||
"java", | "java", | ||||
"-cp", | "--class-path", | ||||
config["classpath"], | config["classpath"], | ||||
*config["java_tool_options"].split(), | *config["java_tool_options"].split(), | ||||
"org.softwareheritage.graph.rpc.GraphServer", | "org.softwareheritage.graph.rpc.GraphServer", | ||||
"--port", | "--port", | ||||
str(port), | str(port), | ||||
str(config["graph"]["path"]), | str(config["path"]), | ||||
] | ] | ||||
print(cmd) | print(cmd) | ||||
# XXX: shlex.join() is in 3.8 | # XXX: shlex.join() is in 3.8 | ||||
# logging.info("Starting RPC server: %s", shlex.join(cmd)) | # logging.info("Starting RPC server: %s", shlex.join(cmd)) | ||||
logging.info("Starting RPC server: %s", " ".join(shlex.quote(x) for x in cmd)) | logging.info("Starting GRPC server: %s", " ".join(shlex.quote(x) for x in cmd)) | ||||
server = subprocess.Popen(cmd) | server = subprocess.Popen(cmd) | ||||
return server, port | return server, port | ||||
def stop_java_rpc_server(server: subprocess.Popen, timeout: int = 15): | def stop_java_grpc_server(server: subprocess.Popen, timeout: int = 15): | ||||
server.terminate() | server.terminate() | ||||
try: | try: | ||||
server.wait(timeout=timeout) | server.wait(timeout=timeout) | ||||
except subprocess.TimeoutExpired: | except subprocess.TimeoutExpired: | ||||
logging.warning("Server did not terminate, sending kill signal...") | logging.warning("Server did not terminate, sending kill signal...") | ||||
server.kill() | server.kill() |