From 979c67d0643880f999df969a8ca229f1a488020f Mon Sep 17 00:00:00 2001 From: Andrew McManus Date: Sun, 25 Jul 2021 17:04:10 -0400 Subject: [PATCH] Added db setter for stats field (updating) so we can disable start button when server is updating. --- app/classes/shared/models.py | 10 ++++++++ app/classes/shared/server.py | 25 ++++++++++++++++--- app/frontend/templates/panel/dashboard.html | 7 +++--- app/frontend/templates/panel/server_term.html | 9 ++++++- app/translations/en_EN.json | 3 ++- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/app/classes/shared/models.py b/app/classes/shared/models.py index 99ac6454..ee7a5292 100644 --- a/app/classes/shared/models.py +++ b/app/classes/shared/models.py @@ -157,6 +157,7 @@ class Server_Stats(BaseModel): players = CharField(default="") desc = CharField(default="Unable to Connect") version = CharField(default="") + updating = BooleanField(default=False) class Meta: @@ -817,6 +818,15 @@ class db_shortcuts: } return conf + @staticmethod + def set_update(server_id, value): + try: + row = Server_Stats.select().where(Server_Stats.server_id == server_id) + except Exception as ex: + logger.error("Database entry not found. ".format(ex)) + with database.atomic(): + Server_Stats.update(updating=value).where(Server_Stats.server_id == server_id).execute() + @staticmethod def set_backup_config(server_id: int, backup_path: str = None, max_backups: int = None, auto_enabled: bool = True): logger.debug("Updating server {} backup config with {}".format(server_id, locals())) diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index bbd618cf..6ac2cfe2 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -16,6 +16,7 @@ from threading import Thread from app.classes.shared.helpers import helper from app.classes.shared.console import console from app.classes.shared.models import db_helper, Servers +from app.classes.web.websocket_helper import websocket_helper logger = logging.getLogger(__name__) @@ -122,6 +123,9 @@ class Server: logger.error("Server is already running - Cancelling Startup") console.error("Server is already running - Cancelling Startup") return False + if self.settings['updating']: + websocket_helper.broadcast('warn', "Server is updating. Not starting. Please wait for update to finish.") + return False logger.info("Launching Server {} with command {}".format(self.name, self.server_command)) console.info("Launching Server {} with command {}".format(self.name, self.server_command)) @@ -379,6 +383,11 @@ class Server: return [] def jar_update(self): + db_helper.set_update(self.server_id, True) + update_thread = threading.Thread(target=self.a_jar_update, daemon=True, name="exe_update") + update_thread.start() + + def a_jar_update(self): wasStarted = "-1" self.backup_server() #checks if server is running. Calls shutdown if it is running. @@ -394,7 +403,7 @@ class Server: if os.path.isdir(backup_dir): backup_executable = os.path.join(backup_dir, 'old_server.jar') else: - logger.info("Executable backup directory not found for Server: {}}. Creating one.".format(self.name)) + logger.info("Executable backup directory not found for Server: {}. Creating one.".format(self.name)) os.mkdir(backup_dir) backup_executable = os.path.join(backup_dir, 'old_server.jar') @@ -415,10 +424,18 @@ class Server: downloaded = helper.download_file(self.settings['executable_update_url'], current_executable) if downloaded: - logger.info("Executable updated successfully.") + while self.is_backingup: + db_helper.set_update(self.server_id, True) + pass + logger.info("Executable updated successfully. Starting Server") + time.sleep(5) + db_helper.set_update(self.server_id, False) + websocket_helper.broadcast('notification', "Executable update finished for "+self.name) + db_helper.add_to_audit_log_raw('Alert', '-1', self.server_id, "Executable update finished for "+self.name, self.settings['server_ip']) if wasStarted: - while self.is_backingup: - pass self.start_server() else: + time.sleep(5) + db_helper.set_update(self.server_id, False) + websocket_helper.broadcast('notification', "Executable update failed for " + self.name+". Check log file for details.") logger.error("Executable download failed.") diff --git a/app/frontend/templates/panel/dashboard.html b/app/frontend/templates/panel/dashboard.html index a3ee9085..84b56a7d 100644 --- a/app/frontend/templates/panel/dashboard.html +++ b/app/frontend/templates/panel/dashboard.html @@ -124,13 +124,14 @@ - {% if server['stats']['running'] %}     + {% elif server['stats']['updating']%} + UPDATING... {% else %} -   -   +   +   {% end %} diff --git a/app/frontend/templates/panel/server_term.html b/app/frontend/templates/panel/server_term.html index 196c56da..f7f41af7 100644 --- a/app/frontend/templates/panel/server_term.html +++ b/app/frontend/templates/panel/server_term.html @@ -83,12 +83,19 @@ - + {% if data['server_stats']['updating']%} +
+ + + +
+ {% else %}
+ {% end %} diff --git a/app/translations/en_EN.json b/app/translations/en_EN.json index da6bffe1..821d2000 100644 --- a/app/translations/en_EN.json +++ b/app/translations/en_EN.json @@ -125,7 +125,8 @@ "sendCommand": "Send command", "start": "Start", "restart": "Restart", - "stop": "Stop" + "stop": "Stop", + "updating": "Updating..." }, "serverPlayerManagement": { "players": "Players",