diff --git a/connpy/_version.py b/connpy/_version.py index 19c844e..31feb3b 100644 --- a/connpy/_version.py +++ b/connpy/_version.py @@ -1 +1 @@ -__version__ = "5.1b2" +__version__ = "5.1b3" diff --git a/connpy/api.py b/connpy/api.py index a9bd940..91edece 100755 --- a/connpy/api.py +++ b/connpy/api.py @@ -56,10 +56,14 @@ def debug_api(port=8048, config=None): server.stop(0) def start_server(port=8048, config=None): - from .grpc.server import serve - conf = config or configfile() - server = serve(conf, port=port, debug=False) - _wait_for_termination() + try: + from connpy.grpc.server import serve + conf = config or configfile() + server = serve(conf, port=port, debug=False) + _wait_for_termination() + except Exception as e: + printer.error(f"Background API failed to start: {e}") + os._exit(1) def start_api(port=8048, config=None): # Check if already running via PID file verification @@ -70,6 +74,7 @@ def start_api(port=8048, config=None): pid = int(f.readline().strip()) os.kill(pid, 0) # If we get here, process exists + printer.info(f"API is already running (PID {pid})") return except (ValueError, OSError, ProcessLookupError): # Stale PID file, ignore here, start_api will overwrite @@ -77,8 +82,11 @@ def start_api(port=8048, config=None): pid = os.fork() if pid == 0: + # Child process: detached from terminal + os.setsid() start_server(port, config=config) else: + # Parent process: record PID and exit try: with open(PID_FILE1, "w") as f: f.write(str(pid) + "\n" + str(port)) @@ -89,4 +97,4 @@ def start_api(port=8048, config=None): except OSError: printer.error("Couldn't create PID file.") exit(1) - printer.start(f"gRPC Server is running with process ID {pid} on port {port}") + printer.start(f"gRPC Server started with process ID {pid} on port {port}") diff --git a/connpy/services/system_service.py b/connpy/services/system_service.py index e64d562..ab74441 100644 --- a/connpy/services/system_service.py +++ b/connpy/services/system_service.py @@ -6,7 +6,6 @@ class SystemService(BaseService): def start_api(self, port=None): """Start the Connpy REST API.""" - print(f"DEBUG SystemService: port type={type(port)} value={port}") from connpy.api import start_api try: start_api(port, config=self.config)