Skip to content
Snippets Groups Projects
Commit 6b930170 authored by Julian Rother's avatar Julian Rother
Browse files

Implemented job canceling

parent 1ec90e26
No related branches found
No related tags found
No related merge requests found
...@@ -296,7 +296,8 @@ CREATE TABLE IF NOT EXISTS `jobs` ( ...@@ -296,7 +296,8 @@ CREATE TABLE IF NOT EXISTS `jobs` (
`last_ping` datetime NOT NULL DEFAULT '', `last_ping` datetime NOT NULL DEFAULT '',
`worker` text DEFAULT NULL, `worker` text DEFAULT NULL,
`data` text NOT NULL DEFAULT '{}', `data` text NOT NULL DEFAULT '{}',
`status` text NOT NULL DEFAULT '{}' `status` text NOT NULL DEFAULT '{}',
`canceled` INTEGER DEFAULT 0
); );
CREATE TABLE IF NOT EXISTS `responsible` ( CREATE TABLE IF NOT EXISTS `responsible` (
......
...@@ -18,9 +18,16 @@ def job_handler(*types, state='finished'): ...@@ -18,9 +18,16 @@ def job_handler(*types, state='finished'):
def schedule_job(jobtype, data=None, priority=0, queue="default"): def schedule_job(jobtype, data=None, priority=0, queue="default"):
if not data: if not data:
data = {} data = {}
modify('INSERT INTO jobs (type, priority, queue, data, time_created) VALUES (?, ?, ?, ?, ?)', return modify('INSERT INTO jobs (type, priority, queue, data, time_created) VALUES (?, ?, ?, ?, ?)',
jobtype, priority, queue, json.dumps(data, default=date_json_handler), datetime.now()) jobtype, priority, queue, json.dumps(data, default=date_json_handler), datetime.now())
def cancel_job(job_id):
modify('UPDATE jobs SET state = "deleted" WHERE id = ? AND state = "ready"', job_id)
modify('UPDATE jobs SET canceled = 1 WHERE id = ?', job_id)
def restart_job(job_id):
modify('UPDATE jobs SET state = "ready" WHERE id = ? AND state = "failed" AND NOT canceled', job_id)
@app.route('/internal/jobs/overview') @app.route('/internal/jobs/overview')
@register_navbar('Jobs', iconlib='fa', icon='suitcase') @register_navbar('Jobs', iconlib='fa', icon='suitcase')
@mod_required @mod_required
...@@ -61,12 +68,12 @@ def jobs_action(action, jobid=None): ...@@ -61,12 +68,12 @@ def jobs_action(action, jobid=None):
query('UPDATE jobs SET state="deleted" WHERE state = "failed" AND (id = ? OR ? IS NULL)', jobid, jobid) query('UPDATE jobs SET state="deleted" WHERE state = "failed" AND (id = ? OR ? IS NULL)', jobid, jobid)
elif action == 'retry_failed': elif action == 'retry_failed':
query('UPDATE jobs SET state="ready" WHERE state = "failed" AND (id = ? OR ? IS NULL)', jobid, jobid) query('UPDATE jobs SET state="ready" WHERE state = "failed" AND (id = ? OR ? IS NULL)', jobid, jobid)
elif action == 'copy': elif action == 'copy' and jobid:
if jobid:
query("INSERT INTO jobs SELECT NULL, type, priority, queue, 'ready', '', '' , ?, '', NULL, data, '{}' FROM jobs where ID=?;", datetime.now(), jobid) query("INSERT INTO jobs SELECT NULL, type, priority, queue, 'ready', '', '' , ?, '', NULL, data, '{}' FROM jobs where ID=?;", datetime.now(), jobid)
elif action == 'delete': elif action == 'delete' and jobid:
if jobid:
query('UPDATE jobs SET state="deleted" WHERE id = ?', jobid) query('UPDATE jobs SET state="deleted" WHERE id = ?', jobid)
elif action == 'cancel' and jobid:
cancel_job(jobid)
return redirect(request.values.get('ref', url_for('jobs_overview'))) return redirect(request.values.get('ref', url_for('jobs_overview')))
def jobs_api_token_required(func): def jobs_api_token_required(func):
...@@ -118,6 +125,9 @@ def jobs_ping(id): ...@@ -118,6 +125,9 @@ def jobs_ping(id):
func(id, job['type'], json.loads(job['data']), state, json.loads(job['status'])) func(id, job['type'], json.loads(job['data']), state, json.loads(job['status']))
except Exception: except Exception:
traceback.print_exc() traceback.print_exc()
if job['canceled']:
return 'Job canceled', 205
else:
return 'OK', 200 return 'OK', 200
@app.route('/internal/jobs/api/worker/<hostname>/schedule', methods=['POST']) @app.route('/internal/jobs/api/worker/<hostname>/schedule', methods=['POST'])
......
...@@ -486,7 +486,7 @@ def dbstatus(): ...@@ -486,7 +486,7 @@ def dbstatus():
def date_json_handler(obj): def date_json_handler(obj):
return obj.isoformat() if hasattr(obj, 'isoformat') else obj return obj.isoformat() if hasattr(obj, 'isoformat') else obj
from jobs import job_handler, schedule_job from jobs import job_handler, schedule_job, cancel_job, restart_job
from edit import edit_handler from edit import edit_handler
import feeds import feeds
import importer import importer
......
...@@ -192,7 +192,7 @@ ...@@ -192,7 +192,7 @@
<td>{{i.priority}}</td> <td>{{i.priority}}</td>
<td>{{i.worker}}</td> <td>{{i.worker}}</td>
<td>{{i.last_ping}}</td> <td>{{i.last_ping}}</td>
<td>{{i.state}}</td> <td>{{i.state}}{% if i.canceled %} (canceled) {% endif %}</td>
<td>{{i.time_created}}</td> <td>{{i.time_created}}</td>
<td>{{i.time_finished}}</td> <td>{{i.time_finished}}</td>
<td>{{i.time_scheduled}}</td> <td>{{i.time_scheduled}}</td>
...@@ -223,6 +223,12 @@ ...@@ -223,6 +223,12 @@
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</a> </a>
</li> </li>
{% elif i.state == "running" %}
<li>
<a class="btn btn-default" {% if i.canceled %}disabled="disabled"{% endif %} href="{{url_for('jobs_action', action='cancel', jobid=i.id, ref=request.url)}}" title="Abbrechen" style="background-color: red;">
<span class="glyphicon glyphicon-stop" aria-hidden="true"></span>
</a>
</li>
{% endif %} {% endif %}
{% endif %} {% endif %}
</ul> </ul>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment