Add working schedules.

Do not use advanced cron scheduling yet.
Ability to add/delete scheduled jobs.
This commit is contained in:
Andrew 2022-01-11 00:26:33 -05:00
parent 139b506554
commit 14ae7c9a46
4 changed files with 121 additions and 31 deletions

View File

@ -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)

View File

@ -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']:

View File

@ -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 = {

View File

@ -48,17 +48,17 @@
<option value="advanced">Advanced</option>
</select>
</div>
<div id="ifBasic">
<div class="form-group">
<label for="server_name">Action<small class="text-muted ml-1"></small> </label><br>
<select id="action" name="action" onchange="yesnoCheck(this);" class="form-control form-control-lg select-css">
<option value="start">Start Server</option>
<option value="restart">Restart Server</option>
<option value="shutdown">Shutdown Server</option>
<option value="backup">Backup Server</option>
<option value="command">Custon Command</option>
</select>
</div>
<div id="ifBasic">
<div class="form-group">
<label for="server_path">Interval <small class="text-muted ml-1"> - How often you want this task to execute</small> </label>
<input type="number" class="form-control" name="interval" id="interval" value="{{ data['server_stats']['server_id']['path'] }}" placeholder="Interval" required>
@ -68,7 +68,6 @@
<option value="days">Days</option>
<option value="hours">Hours</option>
<option value="minutes">Minutes</option>
<option value="weeks">Weeks</option>
</select>
</div>
@ -76,17 +75,17 @@
<label for="time">Time <small class="text-muted ml-1"> - What time do you want your task to execute?</small> </label>
<input type="time" class="form-control" name="time" id="time" value="{{ data['server_stats']['server_id']['log_path'] }}" placeholder="Time">
</div>
<div id="ifYes" style="display: none;">
<div class="form-group">
<label for="command">Command <small class="text-muted ml-1"> - What command do you want us to execute? Do not include the '/'</small> </label>
<input type="input" class="form-control" name="command" id="command" value="" placeholder="Command">
</div>
</div>
</div>
<div id="ifYes" style="display: none;">
<div class="form-group">
<label for="command">Command <small class="text-muted ml-1"> - What command do you want us to execute? Do not include the '/'</small> </label>
<input type="input" class="form-control" name="command" id="command" value="" placeholder="Command">
</div>
</div>
<div id="ifAdvanced" style="display: none;">
<div class="form-group">
<label for="cron">Cron <small class="text-muted ml-1"> - Input your cron string</small> </label>
<input type="input" class="form-control" name="cron" id="cron" value="" placeholder="* * * * backup_server">
<input type="input" class="form-control" name="cron" id="cron" value="" placeholder="* * * * *">
</div>
</div>
@ -108,7 +107,7 @@
</form>
</div>
<div class="col-md-8 col-sm-12">
<div class="col-md-8 col-sm-12" style="overflow-x:auto;">
<div class="card">
<div class="card-body">
<h4 class="card-title">Scheduled Tasks</h4>
@ -147,8 +146,14 @@
</td>
{% end %}
<td id="{{schedule.action}}" class="action">
<a href="/panel/edit_schedule?id={{schedule.schedule_id}}"><i class="fas fa-pencil-alt"></i></a>
<button onclick="window.location.href='/panel/edit_schedule?id={{schedule.schedule_id}}'" class="btn btn-info">
<i class="fas fa-pencil-alt"></i>
</button>
<br>
<br>
<button data-sch={{ schedule.schedule_id }} class="btn btn-danger del_button">
<i class="fas fa-trash" aria-hidden="true"></i>
</button>
</td>
</tr>
{% 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: '<i class="fas fa-times"></i> {{ translate("serverBackups", "cancel", data['lang']) }}'
},
confirm: {
label: '<i class="fas fa-check"></i> {{ 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();
},
});
}
</script>
{% end %}