Run register_service in a separate thread in backend.http.

The Zeroconf registration part may randomly get stuck, resulting in the
web server not being properly started.

It's therefore better to run the Zeroconf registration process
asynchronously, for it's not strictly required for the web server to
execute.
This commit is contained in:
Fabio Manganiello 2023-01-01 13:16:46 +01:00
parent 84ce31cab0
commit dd3c4b10c7
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -251,6 +251,7 @@ class HttpBackend(Backend):
self.disable_websocket = disable_websocket
self.websocket_thread = None
self._websocket_loop = None
self._service_registry_thread = None
self.bind_address = bind_address
if resource_dirs:
@ -328,6 +329,10 @@ class HttpBackend(Backend):
self._websocket_loop.stop()
self.logger.info('HTTP websocket service terminated')
if self._service_registry_thread and self._service_registry_thread.is_alive():
self._service_registry_thread.join(timeout=5)
self._service_registry_thread = None
def _acquire_websocket_lock(self, ws):
try:
acquire_ok = self._websocket_lock.acquire(
@ -453,14 +458,16 @@ class HttpBackend(Backend):
return proc
def run(self):
super().run()
def _register_service(self):
try:
self.register_service(port=self.port)
except Exception as e:
self.logger.warning('Could not register the Zeroconf service')
self.logger.exception(e)
def run(self):
super().run()
if not self.disable_websocket:
self.logger.info('Initializing websocket interface')
self.websocket_thread = threading.Thread(target=self.websocket)
@ -483,5 +490,8 @@ class HttpBackend(Backend):
+ 'for the webserver (e.g. nginx)'
)
self._service_registry_thread = threading.Thread(target=self._register_service)
self._service_registry_thread.start()
# vim:sw=4:ts=4:et: