nopaque/web/app/jobs/tasks.py

37 lines
1.3 KiB
Python

from .. import db, socketio
from ..decorators import background
from ..models import Job
@background
def delete_job(job_id, *args, **kwargs):
with kwargs['app'].app_context():
job = Job.query.get(job_id)
if job is None:
raise Exception('Job {} not found'.format(job_id))
event = 'user_{}_patch'.format(job.user_id)
jsonpatch = [{'op': 'remove', 'path': '/jobs/{}'.format(job.id)}]
room = 'user_{}'.format(job.user_id)
job.delete()
db.session.commit()
socketio.emit(event, jsonpatch, room=room)
@background
def restart_job(job_id, *args, **kwargs):
with kwargs['app'].app_context():
job = Job.query.get(job_id)
if job is None:
raise Exception('Job {} not found'.format(job_id))
try:
job.restart()
except Exception:
pass
else:
db.session.commit()
event = 'user_{}_patch'.format(job.user_id)
jsonpatch = [{'op': 'replace', 'path': '/jobs/{}/end_date'.format(job.id), 'value': job.end_date.timestamp()}, # noqa
{'op': 'replace', 'path': '/jobs/{}/status'.format(job.id), 'value': job.status}] # noqa
room = 'user_{}'.format(job.user_id)
socketio.emit(event, jsonpatch, room=room)