From 074e6117050916bc4752dae0daf2078f202ae1d5 Mon Sep 17 00:00:00 2001 From: PhracturedBlue Date: Wed, 23 Aug 2023 20:52:05 -0700 Subject: [PATCH] Add support for systemd socket-activation --- cps/server.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cps/server.py b/cps/server.py index ed3b7716..652c009c 100644 --- a/cps/server.py +++ b/cps/server.py @@ -27,6 +27,7 @@ try: from gevent.pywsgi import WSGIServer from .gevent_wsgi import MyWSGIHandler from gevent.pool import Pool + from gevent.socket import socket as GeventSocket from gevent import __version__ as _version from greenlet import GreenletExit import ssl @@ -95,6 +96,11 @@ class WebServer(object): log.warning('Cert path: %s', certfile_path) log.warning('Key path: %s', keyfile_path) + def _make_gevent_socket_activated(self): + # Reuse an already open socket on fd=SD_LISTEN_FDS_START + SD_LISTEN_FDS_START = 3 + return GeventSocket(fileno=SD_LISTEN_FDS_START) + def _make_gevent_unix_socket(self, socket_file): # the socket file must not exist prior to bind() if os.path.exists(socket_file): @@ -115,6 +121,10 @@ class WebServer(object): def _make_gevent_socket(self): if os.name != 'nt': + socket_activated = os.environ.get("LISTEN_FDS") + if socket_activated: + sock = self._make_gevent_socket_activated() + return sock, sock.getsockname() unix_socket_file = os.environ.get("CALIBRE_UNIX_SOCKET") if unix_socket_file: return self._make_gevent_unix_socket(unix_socket_file), "unix:" + unix_socket_file