2021-02-01 12:51:07 +01:00
|
|
|
from .. import db, socketio
|
2020-04-21 18:34:21 +02:00
|
|
|
from ..decorators import background
|
|
|
|
from ..models import Job
|
|
|
|
|
|
|
|
|
|
|
|
@background
|
2020-04-23 08:35:18 +02:00
|
|
|
def delete_job(job_id, *args, **kwargs):
|
2020-07-10 11:36:54 +02:00
|
|
|
with kwargs['app'].app_context():
|
2020-04-21 18:34:21 +02:00
|
|
|
job = Job.query.get(job_id)
|
|
|
|
if job is None:
|
2020-07-10 11:36:54 +02:00
|
|
|
raise Exception('Job {} not found'.format(job_id))
|
2021-02-01 12:51:07 +01:00
|
|
|
event = 'user_{}_patch'.format(job.user_id)
|
|
|
|
jsonpatch = [{'op': 'remove', 'path': '/jobs/{}'.format(job.id)}]
|
|
|
|
room = 'user_{}'.format(job.user_id)
|
2020-04-21 18:34:21 +02:00
|
|
|
job.delete()
|
2020-07-10 11:36:54 +02:00
|
|
|
db.session.commit()
|
2021-02-01 12:51:07 +01:00
|
|
|
socketio.emit(event, jsonpatch, room=room)
|
2020-07-09 09:42:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
@background
|
|
|
|
def restart_job(job_id, *args, **kwargs):
|
2020-07-10 11:36:54 +02:00
|
|
|
with kwargs['app'].app_context():
|
2020-07-09 09:42:30 +02:00
|
|
|
job = Job.query.get(job_id)
|
|
|
|
if job is None:
|
2020-07-10 11:36:54 +02:00
|
|
|
raise Exception('Job {} not found'.format(job_id))
|
2021-02-01 12:51:07 +01:00
|
|
|
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)
|