Add job restart url /jobs/job_id/restart

This commit is contained in:
Patrick Jentsch 2020-07-09 09:42:30 +02:00
parent 43f79291aa
commit 53b4b57b61
3 changed files with 52 additions and 2 deletions

View File

@ -1,5 +1,5 @@
from time import sleep from time import sleep
from .. import db from .. import db, logger
from ..decorators import background from ..decorators import background
from ..models import Job from ..models import Job
import os import os
@ -27,3 +27,24 @@ def delete_job(job_id, *args, **kwargs):
'jobs', str(job.id)) 'jobs', str(job.id))
shutil.rmtree(path, ignore_errors=True) shutil.rmtree(path, ignore_errors=True)
job.delete() job.delete()
@background
def restart_job(job_id, *args, **kwargs):
app = kwargs['app']
with app.app_context():
job = Job.query.get(job_id)
if job is None:
logger.warning('Job not found')
return
if job.status != 'failed':
logger.warning('Job not failed')
return
logger.warning('Restarted')
job_dir = os.path.join(app.config['NOPAQUE_STORAGE'],
str(job.user_id),
'jobs',
str(job.id))
shutil.rmtree(os.path.join(job_dir, 'output'), ignore_errors=True)
shutil.rmtree(os.path.join(job_dir, 'pyflow.data'), ignore_errors=True)
job.restart()

View File

@ -3,6 +3,7 @@ from flask import (abort, current_app, flash, redirect, render_template,
from flask_login import current_user, login_required from flask_login import current_user, login_required
from . import jobs from . import jobs
from . import tasks from . import tasks
from ..decorators import admin_required
from ..models import Job, JobInput, JobResult from ..models import Job, JobInput, JobResult
import os import os
@ -49,6 +50,23 @@ def download_job_input(job_id, job_input_id):
filename=job_input.filename) filename=job_input.filename)
@jobs.route('/<int:job_id>/restart')
@login_required
@admin_required
def restart(job_id):
job = Job.query.get_or_404(job_id)
if job.status != 'failed':
flash('Job can not be restarted!', 'job')
else:
tasks.restart_job(job_id)
flash('Job has been restarted!', 'job')
job_inputs = [dict(filename=input.filename,
id=input.id,
job_id=job.id)
for input in job.inputs]
return redirect(url_for('jobs.job', job_id=job_id))
@jobs.route('/<int:job_id>/results/<int:job_result_id>/download') @jobs.route('/<int:job_id>/results/<int:job_result_id>/download')
@login_required @login_required
def download_job_result(job_id, job_result_id): def download_job_result(job_id, job_result_id):

View File

@ -368,6 +368,17 @@ class Job(db.Model):
db.session.delete(self) db.session.delete(self)
db.session.commit() db.session.commit()
def restart(self):
'''
Restart a job - only if the status is failed
'''
if self.status != 'failed':
return
self.end_date = None
self.status = 'submitted'
db.session.commit()
def to_dict(self): def to_dict(self):
return {'id': self.id, return {'id': self.id,
'user_id': self.user_id, 'user_id': self.user_id,