Commit 3043bbb7 authored by Aurélien Campéas's avatar Aurélien Campéas
Browse files

blueprint: fix task abortion

This is why open-coded sql is a bad idea ...
The Task.abort method does what is needed.
parent 6708fad6ef67
......@@ -174,19 +174,6 @@ def reworkui(engine,
logs = job.logs(fromid=args.from_log_id)
return json.dumps([[lid, line] for lid, line in logs])
@bp.route('/kill_job/<jobid>')
def kill_job(jobid):
job = getjob(engine, jobid)
if job is None:
abort(404, 'NO SUCH JOB')
if job.aborted:
return 'was already aborted'
job.abort()
return 'job terminated'
@bp.route('/list_jobs')
def list_jobs():
with engine.begin() as cn:
......@@ -315,10 +302,14 @@ def reworkui(engine,
@bp.route('/abort-task/<tid>')
def abort_task(tid):
with engine.begin() as cn:
sql = task.update().where(task.c.id == tid
).values(abort=True)
cn.execute(sql)
t = Task.byid(engine, tid)
if t is None:
abort(404, 'NO SUCH JOB')
if t.aborted:
return json.dumps(False)
t.abort()
return json.dumps(True)
@bp.route('/taskerror/<int:taskid>')
......
......@@ -10,6 +10,13 @@
<th>domain</th>
</tr></thead>
<tr>
<td scope="row">3</td>
<td>HOSTNAME</td>
<td>abortme</td>
<td>test_rui.py</td>
<td>default</td>
</tr>
<tr>
<td scope="row">2</td>
<td>HOSTNAME</td>
<td>bad_job</td>
......
import re
from pathlib import Path
import time
from lxml import etree
......@@ -38,6 +39,14 @@ def bad_job(task):
raise Exception('I am a little crasher.')
@api.task
def abortme(task):
while True:
time.sleep(1)
# tests
def test_no_job(client):
res = client.get('/job_status/babar')
assert res.status_code == 404
......@@ -65,6 +74,18 @@ def test_bad_request(engine, client):
assert b'input file is mandatory' in res.body
def test_abort(engine, client):
with workers(engine) as mon:
res = client.put('/new_job/abortme?user=Babar',
upload_files=[('input_file', 'input.xml', b'the file', 'text/xml')])
tid = int(res.body)
t = Task.byid(engine, tid)
assert not t.aborted
res = client.get(f'/abort-task/{tid}')
mon.preemptive_kill()
assert t.aborted
def test_task_life_cycle(engine, client, refresh):
with workers(engine):
tasks = []
......@@ -192,7 +213,7 @@ def test_tasks_table(engine, client, refresh):
t.join()
taskstable.refresh_tasks_file(engine)
res = client.get('/tasks-table-hash?domain=uranus')
assert res.text == 'ce14320fb847e8e7443ffca102315671'
assert res.text == '98cc14b1de9bdd660aaa61f8b2ac4144'
res = client.get('/tasks-table?domain=uranus')
refpath = DATADIR / 'tasks-table-uranus.html'
if refresh:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment