diff --git a/neetbox/cli/parse.py b/neetbox/cli/parse.py index ebbaa18f..7a1a8733 100644 --- a/neetbox/cli/parse.py +++ b/neetbox/cli/parse.py @@ -72,8 +72,8 @@ def version_command(): @click.option( "--port", "-p", help="specify which port to launch", metavar="port", required=False, default=0 ) -@click.option("--debug", "-d", is_flag=True, help="Run with debug mode", default=False) -def serve(port, debug): +@click.option("--detach", "-d", is_flag=True, help="Run in detached mode", default=False) +def serve(port, detach): """serve neetbox server in attached mode""" _try_load_workspace_if_applicable() _daemon_config = get_client_config() @@ -81,9 +81,12 @@ def serve(port, debug): if port: _daemon_config["port"] = port logger.log(f"Launching server using config: {_daemon_config}") - from neetbox.server.server_process import server_process + import neetbox.server._daemon_server_launch_script as server_launcher - server_process(cfg=_daemon_config, debug=debug) + if detach: + server_launcher.start(_daemon_config) + else: + server_launcher.run(_daemon_config) except Exception as e: logger.err(f"Failed to launch a neetbox server: {e}", reraise=True) diff --git a/neetbox/client/_client.py b/neetbox/client/_client.py index 5e0d357b..c3b90229 100644 --- a/neetbox/client/_client.py +++ b/neetbox/client/_client.py @@ -189,14 +189,7 @@ def _connect_blocking(self, config=None): ) import neetbox.server._daemon_server_launch_script as server_launcher - popen = DaemonableProcess( # server daemon - target=server_launcher, - args=["--config", json.dumps(config)], - mode=config["mode"], - redirect_stdout=subprocess.DEVNULL if config["mute"] else None, - env_append={"NEETBOX_DAEMON_PROCESS": "1"}, - ).start() - time.sleep(1) + popen = server_launcher.start(config) _retry_timeout = 10 _time_begin = time.perf_counter() logger.debug("Created daemon process, trying to connect to daemon...") diff --git a/neetbox/server/_daemon_server_launch_script.py b/neetbox/server/_daemon_server_launch_script.py index 6090066e..8a424f65 100644 --- a/neetbox/server/_daemon_server_launch_script.py +++ b/neetbox/server/_daemon_server_launch_script.py @@ -1,26 +1,52 @@ import argparse import json import sys +import subprocess +import time +from typing import Optional -def run(argv): - if len(argv) <= 1: - print("_daemon_: Warning: empty daemon_config") - daemon_config = None +def run(config): + from .server_process import server_process + + server_process(config) + + +def start(config) -> Optional[int]: + """start a server as background daemon + + Args: + config (dict): server launch config + + Returns: + DaemonableProcess: the daemon process + """ + from neetbox.utils import DaemonableProcess + import neetbox.server._daemon_server_launch_script as myself + + popen = DaemonableProcess( # server daemon + target=myself, + args=["--config", json.dumps(config)], + mode=config["mode"], + redirect_stdout=subprocess.DEVNULL if config["mute"] else None, + env_append={"NEETBOX_DAEMON_PROCESS": "1"}, + ).start() + time.sleep(1) + return popen + + +if __name__ == "__main__": + print("server starting.\n name:\t", __name__, "\nargs:\t", sys.argv) + if len(sys.argv) <= 1: + print("Warning: empty server config") + config = None else: ap = argparse.ArgumentParser() ap.add_argument("--config") args = ap.parse_args() print(type(args.config)) print(args.config) - daemon_config = json.loads(args.config) - print("Daemon started with config:", daemon_config) - from .server_process import server_process - - server_process(daemon_config) - - -if __name__ == "__main__": - print("daemon server starting.\n name:\t", __name__, "\nargs:\t", sys.argv) - run(sys.argv) - print("daemon server exiting.") + config = json.loads(args.config) + print("Daemon started with config:", config) + run(config) + print("server closed.") diff --git a/pyproject.toml b/pyproject.toml index 4e8dc252..c7faa87f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "neetbox" -version = "0.4.10" +version = "0.4.11" description = "Logging/Debugging/Tracing/Managing/Facilitating long running python projects, especially a replacement of tensorboard for deep learning projects" license = "MIT" authors = ["VisualDust ", "Lideming "] diff --git a/tests/client/neetbox.toml b/tests/client/neetbox.toml index dbc2d3a8..d631b513 100644 --- a/tests/client/neetbox.toml +++ b/tests/client/neetbox.toml @@ -1,5 +1,5 @@ name = "client" -version = "0.4.10" +version = "0.4.11" projectId = "b3219f58-2cb0-482c-b064-85153b4cf86e" [logging]