mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Add single execution to scheduled tasks.
This commit is contained in:
parent
14ae7c9a46
commit
4f55ffed0f
@ -112,6 +112,7 @@ class Schedules(Model):
|
||||
start_time = CharField(null=True)
|
||||
command = CharField(null=True)
|
||||
comment = CharField()
|
||||
one_time = BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
table_name = 'schedules'
|
||||
@ -205,7 +206,7 @@ class helpers_management:
|
||||
# Schedules Methods
|
||||
#************************************************************************************************
|
||||
@staticmethod
|
||||
def create_scheduled_task(server_id, action, interval, interval_type, start_time, command, comment=None, enabled=True):
|
||||
def create_scheduled_task(server_id, action, interval, interval_type, start_time, command, comment=None, enabled=True, one_time=False):
|
||||
sch_id = Schedules.insert({
|
||||
Schedules.server_id: server_id,
|
||||
Schedules.action: action,
|
||||
@ -214,7 +215,8 @@ class helpers_management:
|
||||
Schedules.interval_type: interval_type,
|
||||
Schedules.start_time: start_time,
|
||||
Schedules.command: command,
|
||||
Schedules.comment: comment
|
||||
Schedules.comment: comment,
|
||||
Schedules.one_time: one_time
|
||||
}).execute()
|
||||
return sch_id
|
||||
|
||||
@ -235,6 +237,10 @@ class helpers_management:
|
||||
def get_scheduled_task(schedule_id):
|
||||
return model_to_dict(Schedules.get(Schedules.schedule_id == schedule_id)).execute()
|
||||
|
||||
@staticmethod
|
||||
def get_scheduled_task_model(schedule_id):
|
||||
return Schedules.select().where(Schedules.schedule_id == schedule_id).get()
|
||||
|
||||
@staticmethod
|
||||
def get_schedules_by_server(server_id):
|
||||
return Schedules.select().where(Schedules.server_id == server_id).execute()
|
||||
|
@ -165,8 +165,8 @@ class TasksManager:
|
||||
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='-1')
|
||||
self.scheduler.add_listener(self.schedule_watcher, mask=EVENT_JOB_EXECUTED)
|
||||
#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':
|
||||
@ -181,7 +181,7 @@ class TasksManager:
|
||||
|
||||
|
||||
def schedule_job(self, job_data):
|
||||
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'])
|
||||
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'], "None", job_data['enabled'], job_data['one_time'])
|
||||
if job_data['enabled']:
|
||||
if job_data['interval_type'] == 'hours':
|
||||
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']])
|
||||
@ -200,13 +200,13 @@ class TasksManager:
|
||||
|
||||
def schedule_watcher(self, event):
|
||||
if not event.exception:
|
||||
print(event.job_id, event.code)
|
||||
job = self.scheduler.get_job(event.job_id)
|
||||
trigger = job.trigger
|
||||
if trigger.interval:
|
||||
print(trigger.interval)
|
||||
else:
|
||||
print('well that failed')
|
||||
task = management_helper.get_scheduled_task_model(int(event.job_id))
|
||||
if task.one_time:
|
||||
self.remove_job(task.schedule_id)
|
||||
logger.info("one time task detected. Deleting...")
|
||||
else:
|
||||
print("error")
|
||||
logger.error("Task failed with error: {}".format(event.exception))
|
||||
|
||||
def start_stats_recording(self):
|
||||
stats_update_frequency = helper.get_setting('stats_update_frequency')
|
||||
|
@ -862,15 +862,6 @@ class PanelHandler(BaseHandler):
|
||||
command = "restart_server"
|
||||
elif action == "backup":
|
||||
command = "backup_server"
|
||||
|
||||
if bleach.clean(self.get_argument('enabled', '1')):
|
||||
enabled = True
|
||||
else:
|
||||
enabled = False
|
||||
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))
|
||||
@ -884,6 +875,14 @@ class PanelHandler(BaseHandler):
|
||||
command = "restart_server"
|
||||
elif action == "backup":
|
||||
command = "backup_server"
|
||||
if bleach.clean(self.get_argument('enabled', '1'))=='1':
|
||||
enabled = True
|
||||
else:
|
||||
enabled = False
|
||||
if bleach.clean(self.get_argument('one_time', '0')) == '1':
|
||||
one_time = True
|
||||
else:
|
||||
one_time = False
|
||||
|
||||
if not exec_user['superuser'] and not permissions['Backup'] in user_perms:
|
||||
self.redirect("/panel/error?error=Unauthorized access: User not authorized")
|
||||
@ -898,7 +897,6 @@ class PanelHandler(BaseHandler):
|
||||
return
|
||||
|
||||
if interval_type == "days":
|
||||
print("in job data")
|
||||
job_data = {
|
||||
"server_id": server_id,
|
||||
"action": action,
|
||||
@ -919,7 +917,6 @@ class PanelHandler(BaseHandler):
|
||||
"one_time": one_time
|
||||
}
|
||||
else:
|
||||
print("in job data")
|
||||
job_data = {
|
||||
"server_id": server_id,
|
||||
"action": action,
|
||||
@ -930,7 +927,6 @@ class PanelHandler(BaseHandler):
|
||||
"time": '00:00',
|
||||
"one_time": one_time
|
||||
}
|
||||
print(job_data['time'])
|
||||
|
||||
self.tasks_manager.schedule_job(job_data)
|
||||
|
||||
|
16
app/migrations/20210915205501_one_time_task.py
Normal file
16
app/migrations/20210915205501_one_time_task.py
Normal file
@ -0,0 +1,16 @@
|
||||
# Generated by database migrator
|
||||
import peewee
|
||||
|
||||
def migrate(migrator, database, **kwargs):
|
||||
migrator.add_columns('schedules', one_time=peewee.BooleanField(default=False))
|
||||
"""
|
||||
Write your migrations here.
|
||||
"""
|
||||
|
||||
|
||||
|
||||
def rollback(migrator, database, **kwargs):
|
||||
migrator.drop_columns('schedules', ['one_time'])
|
||||
"""
|
||||
Write your rollback migrations here.
|
||||
"""
|
Loading…
Reference in New Issue
Block a user