Skip to content

Commit

Permalink
added detach mode to cli serve command
Browse files Browse the repository at this point in the history
  • Loading branch information
visualDust committed May 24, 2024
1 parent c49073c commit 4b84304
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
11 changes: 7 additions & 4 deletions neetbox/cli/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,21 @@ 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()
try:
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)

Expand Down
9 changes: 1 addition & 8 deletions neetbox/client/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down
56 changes: 41 additions & 15 deletions neetbox/server/_daemon_server_launch_script.py
Original file line number Diff line number Diff line change
@@ -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.")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>", "Lideming <[email protected]>"]
Expand Down
2 changes: 1 addition & 1 deletion tests/client/neetbox.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "client"
version = "0.4.10"
version = "0.4.11"
projectId = "b3219f58-2cb0-482c-b064-85153b4cf86e"

[logging]
Expand Down

0 comments on commit 4b84304

Please sign in to comment.