Source code for swh.graph.grpc_server
# Copyright (C) 2021-2023 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
"""
A simple tool to start the swh-graph GRPC server in Java.
"""
import logging
import shlex
import subprocess
import aiohttp.test_utils
import aiohttp.web
from swh.graph.config import check_config
logger = logging.getLogger(__name__)
[docs]
def build_java_grpc_server_cmdline(**config):
port = config.pop("port", None)
if port is None:
port = aiohttp.test_utils.unused_port()
logger.debug("Port not configured, using random port %s", port)
logger.debug("Checking configuration and populating default values")
config = check_config(config)
logger.debug("Configuration: %r", config)
cmd = [
"java",
"--class-path",
config["classpath"],
*config["java_tool_options"].split(),
"org.softwareheritage.graph.rpc.GraphServer",
"--port",
str(port),
str(config["path"]),
]
return cmd, port
[docs]
def build_rust_grpc_server_cmdline(**config):
port = config.pop("port", None)
if port is None:
port = aiohttp.test_utils.unused_port()
logger.debug("Port not configured, using random port %s", port)
if config.get("debug", False):
cmd = ["./target/debug/swh-graph-grpc-serve"]
else:
cmd = ["./target/release/swh-graph-grpc-serve"]
if config.get("debug"):
cmd.append("-vvvvv")
else:
cmd.append("-vv")
logger.debug("Checking configuration and populating default values")
config = check_config(config)
logger.debug("Configuration: %r", config)
cmd.extend(["--bind", f"[::]:{port}", str(config["path"])])
return cmd, port
[docs]
def spawn_java_grpc_server(**config):
cmd, port = build_java_grpc_server_cmdline(**config)
print(cmd)
# XXX: shlex.join() is in 3.8
# logger.info("Starting gRPC server: %s", shlex.join(cmd))
logger.info("Starting gRPC server: %s", " ".join(shlex.quote(x) for x in cmd))
server = subprocess.Popen(cmd)
return server, port
[docs]
def spawn_rust_grpc_server(**config):
cmd, port = build_rust_grpc_server_cmdline(**config)
print(cmd)
# XXX: shlex.join() is in 3.8
# logger.info("Starting gRPC server: %s", shlex.join(cmd))
logger.info("Starting gRPC server: %s", " ".join(shlex.quote(x) for x in cmd))
server = subprocess.Popen(cmd)
return server, port
[docs]
def stop_java_grpc_server(server: subprocess.Popen, timeout: int = 15):
server.terminate()
try:
server.wait(timeout=timeout)
except subprocess.TimeoutExpired:
logger.warning("Server did not terminate, sending kill signal...")
server.kill()