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