2020-04-21 16:34:21 +00:00
|
|
|
from time import sleep
|
2020-07-09 07:42:30 +00:00
|
|
|
from .. import db, logger
|
2020-04-21 16:34:21 +00:00
|
|
|
from ..decorators import background
|
|
|
|
from ..models import Job
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
|
|
|
|
@background
|
2020-04-23 06:35:18 +00:00
|
|
|
def delete_job(job_id, *args, **kwargs):
|
|
|
|
app = kwargs['app']
|
2020-04-21 16:34:21 +00:00
|
|
|
with app.app_context():
|
|
|
|
job = Job.query.get(job_id)
|
|
|
|
if job is None:
|
|
|
|
return
|
|
|
|
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()
|
2020-07-09 07:42:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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()
|