First attempt at socket activation
This commit is contained in:
100
server.py
100
server.py
@@ -37,7 +37,7 @@ from app.user_manager import UserManager
|
|||||||
from app.model_manager import ModelFileManager
|
from app.model_manager import ModelFileManager
|
||||||
from app.custom_node_manager import CustomNodeManager
|
from app.custom_node_manager import CustomNodeManager
|
||||||
from app.subgraph_manager import SubgraphManager
|
from app.subgraph_manager import SubgraphManager
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union, List, Tuple, Any
|
||||||
from api_server.routes.internal.internal_routes import InternalRoutes
|
from api_server.routes.internal.internal_routes import InternalRoutes
|
||||||
from protocol import BinaryEventTypes
|
from protocol import BinaryEventTypes
|
||||||
|
|
||||||
@@ -1039,29 +1039,85 @@ class PromptServer():
|
|||||||
ssl_ctx.load_cert_chain(certfile=args.tls_certfile,
|
ssl_ctx.load_cert_chain(certfile=args.tls_certfile,
|
||||||
keyfile=args.tls_keyfile)
|
keyfile=args.tls_keyfile)
|
||||||
scheme = "https"
|
scheme = "https"
|
||||||
|
|
||||||
if verbose:
|
|
||||||
logging.info("Starting server\n")
|
# Check for systemd socket activation
|
||||||
for addr in addresses:
|
systemd_sockets = self._get_systemd_sockets()
|
||||||
address = addr[0]
|
|
||||||
port = addr[1]
|
if systemd_sockets:
|
||||||
site = web.TCPSite(runner, address, port, ssl_context=ssl_ctx)
|
# Use systemd sockets instead of the provided addresses
|
||||||
await site.start()
|
|
||||||
|
|
||||||
if not hasattr(self, 'address'):
|
|
||||||
self.address = address #TODO: remove this
|
|
||||||
self.port = port
|
|
||||||
|
|
||||||
if ':' in address:
|
|
||||||
address_print = "[{}]".format(address)
|
|
||||||
else:
|
|
||||||
address_print = address
|
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
logging.info("To see the GUI go to: {}://{}:{}".format(scheme, address_print, port))
|
logging.info("Using systemd socket activation")
|
||||||
|
|
||||||
|
# Start sites using the systemd sockets
|
||||||
|
for sock in systemd_sockets:
|
||||||
|
# Get socket info for logging
|
||||||
|
try:
|
||||||
|
sock_name = sock.getsockname()
|
||||||
|
address = sock_name[0] if sock_name else "unknown"
|
||||||
|
port = sock_name[1] if sock_name else "unknown"
|
||||||
|
except:
|
||||||
|
address = "unknown"
|
||||||
|
port = "unknown"
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
logging.info("To see the GUI go to: {}://{}:{}".format(scheme, address, port))
|
||||||
|
|
||||||
|
# Create site using the socket
|
||||||
|
site = web.SockSite(runner, sock, ssl_context=ssl_ctx)
|
||||||
|
await site.start()
|
||||||
|
|
||||||
|
# Store the socket info for reference
|
||||||
|
self.systemd_sockets = systemd_sockets
|
||||||
|
else:
|
||||||
|
# Fallback to original behavior
|
||||||
|
if verbose:
|
||||||
|
logging.info("Starting server\n")
|
||||||
|
|
||||||
|
for addr in addresses:
|
||||||
|
address = addr[0]
|
||||||
|
port = addr[1]
|
||||||
|
site = web.TCPSite(runner, address, port, ssl_context=ssl_ctx)
|
||||||
|
await site.start()
|
||||||
|
|
||||||
if call_on_start is not None:
|
if not hasattr(self, 'address'):
|
||||||
call_on_start(scheme, self.address, self.port)
|
self.address = address
|
||||||
|
self.port = port
|
||||||
|
|
||||||
|
if ':' in address:
|
||||||
|
address_print = "[{}]".format(address)
|
||||||
|
else:
|
||||||
|
address_print = address
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
logging.info("To see the GUI go to: {}://{}:{}".format(scheme, address_print, port))
|
||||||
|
|
||||||
|
# Call the callback if provided
|
||||||
|
if call_on_start:
|
||||||
|
await call_on_start()
|
||||||
|
|
||||||
|
def _get_systemd_sockets(self):
|
||||||
|
"""Get sockets from systemd socket activation"""
|
||||||
|
sockets = []
|
||||||
|
|
||||||
|
# Check if systemd socket activation is being used
|
||||||
|
if 'LISTEN_FDS' in os.environ and 'LISTEN_PID' in os.environ:
|
||||||
|
listen_fds = int(os.environ['LISTEN_FDS'])
|
||||||
|
listen_pid = int(os.environ['LISTEN_PID'])
|
||||||
|
|
||||||
|
# Verify this is our process
|
||||||
|
if listen_pid == os.getpid():
|
||||||
|
# Create sockets from file descriptors 3, 4, 5, etc.
|
||||||
|
for i in range(listen_fds):
|
||||||
|
try:
|
||||||
|
# File descriptor starts at 3 for systemd
|
||||||
|
fd = 3 + i
|
||||||
|
sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sockets.append(sock)
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Failed to create socket from fd {fd}: {e}")
|
||||||
|
|
||||||
|
return sockets
|
||||||
|
|
||||||
def add_on_prompt_handler(self, handler):
|
def add_on_prompt_handler(self, handler):
|
||||||
self.on_prompt_handlers.append(handler)
|
self.on_prompt_handlers.append(handler)
|
||||||
|
|||||||
Reference in New Issue
Block a user