diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index 6fa08888..95780a35 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -66,9 +66,28 @@ class Helpers: return True else: return False - logger.error("{} does not exits".format(file)) + logger.error("{} does not exist".format(file)) return True + def check_for_old_logs(self, db_helper): + servers = db_helper.get_all_defined_servers() + for server in servers: + logs_path = os.path.split(server['log_path'])[0] + latest_log_file = os.path.split(server['log_path'])[1] + logs_delete_after = int(server['logs_delete_after']) + if logs_delete_after == 0: + continue + + log_files = list(filter( + lambda val: val != latest_log_file, + os.listdir(logs_path) + )) + for log_file in log_files: + log_file_path = os.path.join(logs_path, log_file) + if self.check_file_exists(log_file_path) and \ + self.is_file_older_than_x_days(log_file_path, logs_delete_after): + os.remove(log_file_path) + def get_setting(self, key, default_return=False): try: diff --git a/app/classes/shared/models.py b/app/classes/shared/models.py index e7719051..c995d7ab 100644 --- a/app/classes/shared/models.py +++ b/app/classes/shared/models.py @@ -105,6 +105,7 @@ class Servers(BaseModel): stop_command = CharField(default="stop") server_ip = CharField(default="127.0.0.1") server_port = IntegerField(default=25565) + logs_delete_after = IntegerField(default=0) class Meta: table_name = "servers" diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index 237d850c..02ae1e6d 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -35,6 +35,9 @@ class TasksManager: self.schedule_thread = threading.Thread(target=self.scheduler_thread, daemon=True, name="scheduler") + self.log_watcher_thread = threading.Thread(target=self.log_watcher, daemon=True, name="log_watcher") + self.log_watcher_thread.start() + self.command_thread = threading.Thread(target=self.command_watcher, daemon=True, name="command_watcher") self.command_thread.start() @@ -133,5 +136,10 @@ class TasksManager: logger.info("Scheduling Serverjars.com cache refresh service every 12 hours") schedule.every(12).hours.do(server_jar_obj.refresh_cache) + def log_watcher(self): + console.debug('in log_watcher') + helper.check_for_old_logs(db_helper) + schedule.every(6).hours.do(lambda: helper.check_for_old_logs(db_helper)) + tasks_manager = TasksManager() diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index c380ff5d..616074dd 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -297,6 +297,7 @@ class PanelHandler(BaseHandler): server_port = self.get_argument('server_port', None) auto_start = int(float(self.get_argument('auto_start', '0'))) crash_detection = int(float(self.get_argument('crash_detection', '0'))) + logs_delete_after = int(float(self.get_argument('logs_delete_after', '0'))) subpage = self.get_argument('subpage', None) user_data = json.loads(self.get_secure_cookie("user_data")) @@ -328,6 +329,7 @@ class PanelHandler(BaseHandler): Servers.server_port: server_port, Servers.auto_start: auto_start, Servers.crash_detection: crash_detection, + Servers.logs_delete_after: logs_delete_after, }).where(Servers.server_id == server_id).execute() controller.refresh_server_settings(server_id) diff --git a/app/frontend/templates/panel/server_config.html b/app/frontend/templates/panel/server_config.html index 39d6d0b1..5d7450d2 100644 --- a/app/frontend/templates/panel/server_config.html +++ b/app/frontend/templates/panel/server_config.html @@ -112,6 +112,11 @@ +
+ + +
+