Refactored signaling of restart type on restart

This commit is contained in:
CasVT
2025-08-12 17:28:28 +02:00
parent b5387a0635
commit 344214a605
2 changed files with 19 additions and 14 deletions

View File

@@ -99,9 +99,10 @@ def _main(
tz_change_handler = TimezoneChangeHandler()
tz_change_handler.set_detector_timer()
restart_type = None
try:
# =================
SERVER.run(settings.host, settings.port)
restart_type = SERVER.run(settings.host, settings.port)
# =================
finally:
@@ -109,9 +110,9 @@ def _main(
DatabaseBackupHandler.stop_backup_timer()
tz_change_handler.stop_detector_timer()
if SERVER.start_type is not None:
if restart_type is not None:
LOGGER.info("Restarting MIND")
exit(SERVER.start_type.value)
exit(restart_type.value)
exit(0)

View File

@@ -58,7 +58,7 @@ class Server(metaclass=Singleton):
url_prefix = ''
def __init__(self) -> None:
self.start_type = None
self.__start_type = None
return
def create_app(self) -> None:
@@ -150,22 +150,25 @@ class Server(metaclass=Singleton):
)
return server
def run(self, host: str, port: int) -> None:
def run(self, host: str, port: int) -> Union[StartType, None]:
"""Start the webserver.
Args:
host (str): Where to host the server on (e.g. `0.0.0.0`).
port (int): The port to host the server on (e.g. `5656`).
Returns:
Union[StartType, None]: `None` on shutdown, `StartType` on restart.
"""
self.server = self.__create_waitress_server(host, port)
LOGGER.info(f'MIND running on http://{host}:{port}{self.url_prefix}')
self.server.run()
return
return self.__start_type
def __shutdown_thread_function(self) -> None:
"""Shutdown waitress server. Intended to be run in a thread.
"""
def __trigger_server_shutdown(self) -> None:
"""Shutdown waitress server. Intended to be run in a thread."""
if not hasattr(self, 'server'):
return
@@ -176,27 +179,28 @@ class Server(metaclass=Singleton):
def shutdown(self) -> None:
"""
Stop the waitress server. Starts a thread that shuts down the server.
Stop the waitress server. Starts a thread that will trigger the server
shutdown after one second.
"""
self.get_db_timer_thread(
1.0,
self.__shutdown_thread_function,
self.__trigger_server_shutdown,
"InternalStateHandler"
).start()
return
def restart(
self,
start_type: StartType = StartType.STARTUP
start_type: StartType = StartType.RESTART
) -> None:
"""Same as `self.shutdown()`, but restart instead of shutting down.
Args:
start_type (StartType, optional): Why Kapowarr should
restart.
Defaults to StartType.STARTUP.
Defaults to StartType.RESTART.
"""
self.start_type = start_type
self.__start_type = start_type
self.shutdown()
return