From 14ae7c9a4683d2c0bbbc7734da26a1fb022437f6 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 11 Jan 2022 00:26:33 -0500 Subject: [PATCH] Add working schedules. Do not use advanced cron scheduling yet. Ability to add/delete scheduled jobs. --- app/classes/shared/tasks.py | 24 ++++-- app/classes/web/ajax_handler.py | 7 ++ app/classes/web/panel_handler.py | 47 +++++++++--- .../templates/panel/server_tasks.html | 74 +++++++++++++++---- 4 files changed, 121 insertions(+), 31 deletions(-) diff --git a/app/classes/shared/tasks.py b/app/classes/shared/tasks.py index 97e43f3b..5c351a96 100644 --- a/app/classes/shared/tasks.py +++ b/app/classes/shared/tasks.py @@ -116,6 +116,8 @@ class TasksManager: elif command == "update_executable": svr.jar_update() + else: + svr.send_command(command) management_helper.mark_command_complete(c.get('command_id', None)) time.sleep(1) @@ -160,10 +162,21 @@ class TasksManager: self.realtime_thread.start() def scheduler_thread(self): + schedules = management_helper.get_schedules_enabled() tz = get_localzone() self.scheduler.configure(timezone=tz) self.scheduler.add_listener(self.schedule_watcher, mask=EVENT_ALL) - self.scheduler.add_job(self.scheduler.print_jobs, 'interval', seconds=10, id='1225') + self.scheduler.add_job(self.scheduler.print_jobs, 'interval', seconds=10, id='-1') + #load schedules from DB + for schedule in schedules: + if schedule.interval_type == 'hours': + self.scheduler.add_job(management_helper.add_command, 'cron', minute = 0, hour = '*/'+str(schedule.interval), id=str(schedule.schedule_id), args=[schedule.server_id, self.users_controller.get_id_by_name('system'), '127.0.0.1', schedule.command]) + elif schedule.interval_type == 'minutes': + self.scheduler.add_job(management_helper.add_command, 'cron', minute = '*/'+str(schedule.interval), id=str(schedule.schedule_id), args=[schedule.server_id, self.users_controller.get_id_by_name('system'), '127.0.0.1', schedule.command]) + elif schedule.interval_type == 'days': + time = schedule.start_time.split(':') + self.scheduler.add_job(management_helper.add_command, 'cron', day = '*/'+str(schedule.interval), hour=time[0], minute=time[1], id=str(schedule.schedule_id), args=[schedule.server_id, self.users_controller.get_id_by_name('system'), '127.0.0.1', schedule.command]) + self.scheduler.start() @@ -171,13 +184,12 @@ class TasksManager: sch_id = management_helper.create_scheduled_task(job_data['server_id'], job_data['action'], job_data['interval'], job_data['interval_type'], job_data['time'], job_data['command'], job_data['enabled']) if job_data['enabled']: if job_data['interval_type'] == 'hours': - self.scheduler.add_job(management_helper.add_command, 'interval', hours = int(job_data['interval']), id=str(sch_id), args=[job_data['server_id'], self.users_controller.get_id_by_name('system'), '127.0.0.1', job_data['command']]) + self.scheduler.add_job(management_helper.add_command, 'cron', minute = 0, hour = '*/'+str(job_data['interval']), id=str(sch_id), args=[job_data['server_id'], self.users_controller.get_id_by_name('system'), '127.0.0.1', job_data['command']]) elif job_data['interval_type'] == 'minutes': - self.scheduler.add_job(management_helper.add_command, 'interval', minutes = int(job_data['interval']), id=str(sch_id), args=[job_data['server_id'], self.users_controller.get_id_by_name('system'), '127.0.0.1', job_data['command']]) - elif job_data['interval_type'] == 'seconds': - self.scheduler.add_job(management_helper.add_command, 'interval', seconds = int(job_data['interval']), id=str(sch_id), args=[job_data['server_id'], self.users_controller.get_id_by_name('system'), '127.0.0.1', job_data['command']]) + self.scheduler.add_job(management_helper.add_command, 'cron', minute = '*/'+str(job_data['interval']), id=str(sch_id), args=[job_data['server_id'], self.users_controller.get_id_by_name('system'), '127.0.0.1', job_data['command']]) elif job_data['interval_type'] == 'days': - self.scheduler.add_job(management_helper.add_command, 'interval', days = int(job_data['interval']), start_date = timedelta(job_data['time']), id=str(sch_id), args=[job_data['server_id'], self.users_controller.get_id_by_name('system'), '127.0.0.1', job_data['command']], ) + time = job_data['time'].split(':') + self.scheduler.add_job(management_helper.add_command, 'cron', day = '*/'+str(job_data['interval']), hour = time[0], minute = time[1], id=str(sch_id), args=[job_data['server_id'], self.users_controller.get_id_by_name('system'), '127.0.0.1', job_data['command']], ) def remove_job(self, sch_id): management_helper.delete_scheduled_task(sch_id) diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index 1456d7e4..d8f472aa 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -341,6 +341,13 @@ class AjaxHandler(BaseHandler): # Delete the file os.remove(file_path) + if page == "del_task": + if not permissions['Schedule'] in user_perms: + self.redirect("/panel/error?error=Unauthorized access to Tasks") + else: + sch_id = self.get_argument('schedule_id', '-404') + self.tasks_manager.remove_job(sch_id) + if page == "del_backup": if not permissions['Backup'] in user_perms: if not exec_user['superuser']: diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index 5d35bdb8..c3cbc654 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -841,35 +841,49 @@ class PanelHandler(BaseHandler): if page == "tasks": - server_id = self.get_argument('id', None) - difficulty = self.get_argument('difficulty', None) + server_id = bleach.clean(self.get_argument('id', None)) + difficulty = bleach.clean(self.get_argument('difficulty', None)) server_obj = self.controller.servers.get_server_obj(server_id) - enabled = self.get_argument('enabled', '0') + enabled = bleach.clean(self.get_argument('enabled', '0')) if difficulty == 'basic': - action = self.get_argument('action', None) - interval = self.get_argument('interval', None) - interval_type = self.get_argument('interval_type', None) + action = bleach.clean(self.get_argument('action', None)) + interval = bleach.clean(self.get_argument('interval', None)) + interval_type = bleach.clean(self.get_argument('interval_type', None)) #only check for time if it's number of days if interval_type == "days": - time = self.get_argument('time', None) + time = bleach.clean(self.get_argument('time', None)) if action == "command": - command = self.get_argument('command', None) + command = bleach.clean(self.get_argument('command', None)) elif action == "start": command = "start_server" elif action == "stop": command = "stop_server" elif action == "restart": - command = "restar_server" + command = "restart_server" + elif action == "backup": + command = "backup_server" - if self.get_argument('enabled', '1'): + if bleach.clean(self.get_argument('enabled', '1')): enabled = True else: enabled = False - if self.get_argument('one_time', '0'): + if bleach.clean(self.get_argument('one_time', '0')): one_time = True else: one_time = False - + else: + cron_string = bleach.clean(self.get_argument('cron', '')) + action = bleach.clean(self.get_argument('action', None)) + if action == "command": + command = bleach.clean(self.get_argument('command', None)) + elif action == "start": + command = "start_server" + elif action == "stop": + command = "stop_server" + elif action == "restart": + command = "restart_server" + elif action == "backup": + command = "backup_server" if not exec_user['superuser'] and not permissions['Backup'] in user_perms: self.redirect("/panel/error?error=Unauthorized access: User not authorized") @@ -895,6 +909,15 @@ class PanelHandler(BaseHandler): "enabled": enabled, "one_time": one_time } + elif difficulty == "advanced": + job_data = { + "server_id": server_id, + "action": action, + "command": command, + "cron_string": cron_string, + "enabled": enabled, + "one_time": one_time + } else: print("in job data") job_data = { diff --git a/app/frontend/templates/panel/server_tasks.html b/app/frontend/templates/panel/server_tasks.html index 5d152ad5..af108d37 100644 --- a/app/frontend/templates/panel/server_tasks.html +++ b/app/frontend/templates/panel/server_tasks.html @@ -48,17 +48,17 @@ -

- +
@@ -68,7 +68,6 @@ -
@@ -76,17 +75,17 @@
-
+ @@ -108,7 +107,7 @@ -
+

Scheduled Tasks

@@ -147,8 +146,14 @@ {% end %} - - + +
+
+ {% end %} @@ -203,6 +208,49 @@ } } +$( ".del_button" ).click(function() { + var sch_id = $(this).data('sch'); + var server_id = {{ data['server_stats']['server_id']['server_id'] }}; + + console.log(sch_id) + + bootbox.confirm({ + title: "Test", + message: "{{ translate('serverBackups', 'confirmDelete', data['lang']) }}", + buttons: { + cancel: { + label: ' {{ translate("serverBackups", "cancel", data['lang']) }}' + }, + confirm: { + label: ' {{ translate("serverBackups", "confirm", data['lang']) }}' + } + }, + callback: function (result) { + console.log(result); + if (result == true) { + del_task(sch_id, server_id); + } + } + }); +}); + +function del_task(sch_id, id){ + var token = getCookie("_xsrf") + + $.ajax({ + type: "DELETE", + headers: {'X-XSRFToken': token}, + url: '/ajax/del_task?server_id='+id+'&schedule_id='+sch_id, + data: { + schedule_id: sch_id, + id: id + }, + success: function(data) { + location.reload(); + }, + }); +} + {% end %} \ No newline at end of file