fix: stabilize API startup and bump version to 5.1b3

This commit is contained in:
2026-04-17 21:04:42 -03:00
parent 4dd822a921
commit f30e8a991f
3 changed files with 14 additions and 7 deletions
+1 -1
View File
@@ -1 +1 @@
__version__ = "5.1b2" __version__ = "5.1b3"
+13 -5
View File
@@ -56,10 +56,14 @@ def debug_api(port=8048, config=None):
server.stop(0) server.stop(0)
def start_server(port=8048, config=None): def start_server(port=8048, config=None):
from .grpc.server import serve try:
conf = config or configfile() from connpy.grpc.server import serve
server = serve(conf, port=port, debug=False) conf = config or configfile()
_wait_for_termination() 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): def start_api(port=8048, config=None):
# Check if already running via PID file verification # Check if already running via PID file verification
@@ -70,6 +74,7 @@ def start_api(port=8048, config=None):
pid = int(f.readline().strip()) pid = int(f.readline().strip())
os.kill(pid, 0) os.kill(pid, 0)
# If we get here, process exists # If we get here, process exists
printer.info(f"API is already running (PID {pid})")
return return
except (ValueError, OSError, ProcessLookupError): except (ValueError, OSError, ProcessLookupError):
# Stale PID file, ignore here, start_api will overwrite # Stale PID file, ignore here, start_api will overwrite
@@ -77,8 +82,11 @@ def start_api(port=8048, config=None):
pid = os.fork() pid = os.fork()
if pid == 0: if pid == 0:
# Child process: detached from terminal
os.setsid()
start_server(port, config=config) start_server(port, config=config)
else: else:
# Parent process: record PID and exit
try: try:
with open(PID_FILE1, "w") as f: with open(PID_FILE1, "w") as f:
f.write(str(pid) + "\n" + str(port)) f.write(str(pid) + "\n" + str(port))
@@ -89,4 +97,4 @@ def start_api(port=8048, config=None):
except OSError: except OSError:
printer.error("Couldn't create PID file.") printer.error("Couldn't create PID file.")
exit(1) 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}")
-1
View File
@@ -6,7 +6,6 @@ class SystemService(BaseService):
def start_api(self, port=None): def start_api(self, port=None):
"""Start the Connpy REST API.""" """Start the Connpy REST API."""
print(f"DEBUG SystemService: port type={type(port)} value={port}")
from connpy.api import start_api from connpy.api import start_api
try: try:
start_api(port, config=self.config) start_api(port, config=self.config)