nopaque/web/app/jobs/tasks.py

37 lines
1.3 KiB
Python
Raw Normal View History

from .. import db, socketio
from ..decorators import background
from ..models import Job
@background
def delete_job(job_id, *args, **kwargs):
2020-07-10 09:36:54 +00:00
with kwargs['app'].app_context():
job = Job.query.get(job_id)
if job is None:
2020-07-10 09:36:54 +00:00
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()
2020-07-10 09:36:54 +00:00
db.session.commit()
socketio.emit(event, jsonpatch, room=room)
@background
def restart_job(job_id, *args, **kwargs):
2020-07-10 09:36:54 +00:00
with kwargs['app'].app_context():
job = Job.query.get(job_id)
if job is None:
2020-07-10 09:36:54 +00:00
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)