I think that the "remove old logs" feature is done

This commit is contained in:
LukasDoesDev 2021-01-20 17:20:42 +02:00
parent 30b65d0d48
commit bcaac8b9eb
5 changed files with 36 additions and 1 deletions

View File

@ -61,9 +61,28 @@ class Helpers:
return True return True
else: else:
return False return False
logger.error("{} does not exits".format(file)) logger.error("{} does not exist".format(file))
return True 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): def get_setting(self, key, default_return=False):
try: try:

View File

@ -85,6 +85,7 @@ class Servers(BaseModel):
stop_command = CharField(default="stop") stop_command = CharField(default="stop")
server_ip = CharField(default="127.0.0.1") server_ip = CharField(default="127.0.0.1")
server_port = IntegerField(default=25565) server_port = IntegerField(default=25565)
logs_delete_after = IntegerField(default=0)
class Meta: class Meta:
table_name = "servers" table_name = "servers"

View File

@ -35,6 +35,9 @@ class TasksManager:
self.schedule_thread = threading.Thread(target=self.scheduler_thread, daemon=True, name="scheduler") 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 = threading.Thread(target=self.command_watcher, daemon=True, name="command_watcher")
self.command_thread.start() self.command_thread.start()
@ -133,5 +136,10 @@ class TasksManager:
logger.info("Scheduling Serverjars.com cache refresh service every 12 hours") logger.info("Scheduling Serverjars.com cache refresh service every 12 hours")
schedule.every(12).hours.do(server_jar_obj.refresh_cache) 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() tasks_manager = TasksManager()

View File

@ -157,6 +157,7 @@ class PanelHandler(BaseHandler):
server_port = self.get_argument('server_port', None) server_port = self.get_argument('server_port', None)
auto_start = int(float(self.get_argument('auto_start', '0'))) auto_start = int(float(self.get_argument('auto_start', '0')))
crash_detection = int(float(self.get_argument('crash_detection', '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) subpage = self.get_argument('subpage', None)
if server_id is None: if server_id is None:
@ -182,6 +183,7 @@ class PanelHandler(BaseHandler):
Servers.server_port: server_port, Servers.server_port: server_port,
Servers.auto_start: auto_start, Servers.auto_start: auto_start,
Servers.crash_detection: crash_detection, Servers.crash_detection: crash_detection,
Servers.logs_delete_after: logs_delete_after,
}).where(Servers.server_id == server_id).execute() }).where(Servers.server_id == server_id).execute()
controller.refresh_server_settings(server_id) controller.refresh_server_settings(server_id)

View File

@ -112,6 +112,11 @@
<input type="number" class="form-control" name="server_port" id="server_port" value="{{ data['server_stats'][0]['server_id']['server_port'] }}" step="1" max="65566" min="1" > <input type="number" class="form-control" name="server_port" id="server_port" value="{{ data['server_stats'][0]['server_id']['server_port'] }}" step="1" max="65566" min="1" >
</div> </div>
<div class="form-group">
<label for="logs_delete_after">Remove Old Logs After <small class="text-muted ml-1"> - How many days will a log file has to be old to get deleted (0 is off)</small> </label>
<input type="number" class="form-control" name="logs_delete_after" id="logs_delete_after" value="{{ data['server_stats'][0]['server_id']['logs_delete_after'] }}" step="1" max="365" min="0" >
</div>
<div class="form-check-flat"> <div class="form-check-flat">
<label for="auto_start" class="form-check-label ml-4 mb-4"> <label for="auto_start" class="form-check-label ml-4 mb-4">
{% if data['server_stats'][0]['server_id']['auto_start'] %} {% if data['server_stats'][0]['server_id']['auto_start'] %}