Skip to content

Commit

Permalink
implement restart behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Sep 12, 2024
1 parent 7d91c4e commit ca328b2
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions logprep/util/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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()

0 comments on commit ca328b2

Please sign in to comment.