|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +import os |
| 4 | + |
| 5 | +from library.python.testing.recipe import declare_recipe, set_env |
| 6 | +from library.recipes import common as recipes_common |
| 7 | +from yatest.common.network import PortManager |
| 8 | +from ydb.tests.library.common import yatest_common |
| 9 | + |
| 10 | + |
| 11 | +PID_FILENAME = "kqprun_daemon.pid" |
| 12 | +KQPRUN_PATH = os.getenv("KQPRUN_EXECUTABLE") or "ydb/tests/tools/kqprun/kqprun" |
| 13 | +INITIALIZATION_IMEOUT_RATIO = 2 |
| 14 | + |
| 15 | + |
| 16 | +def is_kqprun_daemon_ready() -> bool: |
| 17 | + with open(yatest_common.output_path("kqprun_daemon.out.log"), "r") as outFile: |
| 18 | + return "Initialization finished" in outFile.read() |
| 19 | + |
| 20 | + |
| 21 | +def build_start_comand(argv: list[str], grpc_port: int) -> tuple[int, list[str]]: |
| 22 | + parser = argparse.ArgumentParser() |
| 23 | + parser.add_argument("--query", action="append", type=str, default=[]) |
| 24 | + parser.add_argument("--config", action='store', type=str, default="ydb/tests/tools/kqprun/kqprun/configuration/app_config.conf") |
| 25 | + parser.add_argument("--timeout-ms", action='store', type=int, default=30000) |
| 26 | + parsed, _ = parser.parse_known_args(argv) |
| 27 | + |
| 28 | + cmd = [ |
| 29 | + yatest_common.binary_path(KQPRUN_PATH), |
| 30 | + "--log-file", yatest_common.output_path("kqprun_daemon.ydb.log"), |
| 31 | + "--app-config", yatest_common.source_path(parsed.config), |
| 32 | + "--grpc", str(grpc_port), |
| 33 | + "--timeout", str(parsed.timeout_ms) |
| 34 | + ] |
| 35 | + |
| 36 | + if parsed.query: |
| 37 | + cmd.append("--execution-case") |
| 38 | + cmd.append("query") |
| 39 | + |
| 40 | + for query in parsed.query: |
| 41 | + cmd.append("--script-query") |
| 42 | + cmd.append(yatest_common.source_path(query)) |
| 43 | + |
| 44 | + return (parsed.timeout_ms, cmd) |
| 45 | + |
| 46 | + |
| 47 | +def start(argv: list[str]): |
| 48 | + logging.debug("Starting kqprun daemon") |
| 49 | + |
| 50 | + portManager = PortManager() |
| 51 | + grpc_port = portManager.get_port() |
| 52 | + timeout_ms, cmd = build_start_comand(argv, grpc_port) |
| 53 | + |
| 54 | + recipes_common.start_daemon( |
| 55 | + command=cmd, |
| 56 | + environment=None, |
| 57 | + is_alive_check=is_kqprun_daemon_ready, |
| 58 | + pid_file_name=PID_FILENAME, |
| 59 | + timeout=INITIALIZATION_IMEOUT_RATIO * (timeout_ms // 1000), |
| 60 | + daemon_name="kqprun_daemon" |
| 61 | + ) |
| 62 | + |
| 63 | + set_env("KQPRUN_ENDPOINT", f"grpc://localhost:{grpc_port}") |
| 64 | + logging.debug(f"kqprun daemon has been started on port: {grpc_port}") |
| 65 | + |
| 66 | + |
| 67 | +def stop(argv: list[str]): |
| 68 | + logging.debug("Stop kqprun daemon") |
| 69 | + with open(PID_FILENAME, "r") as pidFile: |
| 70 | + pid = int(pidFile.read()) |
| 71 | + recipes_common.stop_daemon(pid) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + declare_recipe(start, stop) |
0 commit comments