Move file deletion to model methods. add restart job button.

This commit is contained in:
Patrick Jentsch 2020-07-09 15:07:43 +02:00
parent e20a6a31d9
commit f86f3f4fd5
4 changed files with 32 additions and 34 deletions

View File

@ -1,9 +1,5 @@
from time import sleep
from .. import db, logger
from ..decorators import background from ..decorators import background
from ..models import Job from ..models import Job
import os
import shutil
@background @background
@ -12,20 +8,7 @@ def delete_job(job_id, *args, **kwargs):
with app.app_context(): with app.app_context():
job = Job.query.get(job_id) job = Job.query.get(job_id)
if job is None: if job is None:
return raise Exception('Could not find job with id {}'.format(job_id))
if job.status not in ['complete', 'failed']:
job.status = 'canceling'
db.session.commit()
while job.status != 'canceled':
# In case the daemon handled a job in any way
if job.status != 'canceling':
job.status = 'canceling'
db.session.commit()
sleep(1)
db.session.refresh(job)
path = os.path.join(app.config['NOPAQUE_STORAGE'], str(job.user_id),
'jobs', str(job.id))
shutil.rmtree(path, ignore_errors=True)
job.delete() job.delete()
@ -35,16 +18,5 @@ def restart_job(job_id, *args, **kwargs):
with app.app_context(): with app.app_context():
job = Job.query.get(job_id) job = Job.query.get(job_id)
if job is None: if job is None:
logger.warning('Job not found') raise Exception('Could not find job with id {}'.format(job_id))
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() job.restart()

View File

@ -56,7 +56,7 @@ def download_job_input(job_id, job_input_id):
def restart(job_id): def restart(job_id):
job = Job.query.get_or_404(job_id) job = Job.query.get_or_404(job_id)
if job.status != 'failed': if job.status != 'failed':
flash('Job can not be restarted!', 'job') flash('Could not restart job: status is not "failed"', 'error')
else: else:
tasks.restart_job(job_id) tasks.restart_job(job_id)
flash('Job has been restarted!', 'job') flash('Job has been restarted!', 'job')

View File

@ -2,9 +2,12 @@ from datetime import datetime
from flask import current_app from flask import current_app
from flask_login import UserMixin, AnonymousUserMixin from flask_login import UserMixin, AnonymousUserMixin
from itsdangerous import BadSignature, TimedJSONWebSignatureSerializer from itsdangerous import BadSignature, TimedJSONWebSignatureSerializer
from time import sleep
from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from . import db, login_manager from . import db, login_manager
import os
import shutil
class Permission: class Permission:
@ -364,7 +367,21 @@ class Job(db.Model):
''' '''
Delete the job and its inputs and results from the database. Delete the job and its inputs and results from the database.
''' '''
if self.status not in ['complete', 'failed']:
self.status = 'canceling'
db.session.commit()
while self.status != 'canceled':
# In case the daemon handled a job in any way
if self.status != 'canceling':
self.status = 'canceling'
db.session.commit()
sleep(1)
db.session.refresh(self)
job_dir = os.path.join(current_app.config['NOPAQUE_STORAGE'],
str(self.user_id),
'jobs',
str(self.id))
shutil.rmtree(job_dir, ignore_errors=True)
db.session.delete(self) db.session.delete(self)
db.session.commit() db.session.commit()
@ -374,7 +391,13 @@ class Job(db.Model):
''' '''
if self.status != 'failed': if self.status != 'failed':
return raise Exception('Could not restart job: status is not "failed"')
job_dir = os.path.join(current_app.config['NOPAQUE_STORAGE'],
str(self.user_id),
'jobs',
str(self.id))
shutil.rmtree(os.path.join(job_dir, 'output'), ignore_errors=True)
shutil.rmtree(os.path.join(job_dir, 'pyflow.data'), ignore_errors=True)
self.end_date = None self.end_date = None
self.status = 'submitted' self.status = 'submitted'
db.session.commit() db.session.commit()

View File

@ -71,7 +71,10 @@
</div> </div>
</div> </div>
<div class="card-action right-align"> <div class="card-action right-align">
<a href="#" class="btn disabled waves-effect waves-light"><i class="material-icons left">settings</i>Export Parameters</a> {% if current_user.is_administrator() and job.status == 'failed' %}
<a href="{{ url_for('jobs.restart', job_id=job.id) }}" class="btn waves-effect waves-light"><i class="material-icons left">repeat</i>Restart</a>
{% endif %}
<!-- <a href="#" class="btn disabled waves-effect waves-light"><i class="material-icons left">settings</i>Export Parameters</a> -->
<a data-target="delete-job-modal" class="waves-effect waves-light btn red modal-trigger"><i class="material-icons left">delete</i>Delete</a> <a data-target="delete-job-modal" class="waves-effect waves-light btn red modal-trigger"><i class="material-icons left">delete</i>Delete</a>
</div> </div>
</div> </div>