From ca328b2f0a3866447003f6f68ddb2b4b0fd85937 Mon Sep 17 00:00:00 2001 From: ekneg54 Date: Thu, 12 Sep 2024 17:24:30 +0200 Subject: [PATCH] implement restart behavior --- logprep/util/http.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/logprep/util/http.py b/logprep/util/http.py index 23292a9c6..c56417fd2 100644 --- a/logprep/util/http.py +++ b/logprep/util/http.py @@ -55,6 +55,7 @@ def __init__( if ( hasattr(self, "thread") + and self.thread is not None and self.thread.is_alive() # pylint: disable=access-member-before-definition ): self.shut_down() @@ -70,17 +71,21 @@ def __init__( logprep_log_config = json.loads( os.environ.get("LOGPREP_LOG_CONFIG", json.dumps(DEFAULT_LOG_CONFIG)) ) - uvicorn_config = uvicorn.Config(**uvicorn_config, app=app, log_config=logprep_log_config) + self.uvicorn_config = uvicorn.Config( + **uvicorn_config, app=app, log_config=logprep_log_config + ) logging.getLogger("uvicorn.access").name = self._logger_name logging.getLogger("uvicorn.error").name = self._logger_name - self.server = uvicorn.Server(uvicorn_config) - self.thread = threading.Thread(daemon=daemon, target=self.server.run) + self.server = None + self.thread = None + self.daemon = daemon def start(self): """Collect all configs, initiate application server and webserver and run thread with uvicorn+falcon http server and wait until it is up (started)""" - + self.server = uvicorn.Server(self.uvicorn_config) + self.thread = threading.Thread(daemon=self.daemon, target=self.server.run) self.thread.start() while not self.server.started: continue @@ -95,3 +100,11 @@ def shut_down(self): self._logger.debug("Wait for server to exit gracefully...") continue self.thread.join() + self.server = None + self.thread = None + + def restart(self): + """Restart the server by shutting down the existing server and + starting a new one""" + self.shut_down() + self.start()