69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
"""Queue page and service provider; workers use the internal Python connector."""
|
|
from flask import Blueprint, abort, current_app, flash, g, jsonify, redirect, render_template, request, url_for
|
|
from web_framework.auth import can
|
|
from web_framework.plugins import Plugin, get_plugins
|
|
from .connector import Queue, STATES
|
|
|
|
bp = Blueprint('plugin_queues', __name__, template_folder='templates', static_folder='static')
|
|
|
|
|
|
def connector():
|
|
return get_plugins().service('queues', 'queue')
|
|
|
|
|
|
def selection():
|
|
status = request.args.get('status') or None
|
|
source = request.args.get('source') or None
|
|
try:
|
|
page = int(request.args.get('page', '1'))
|
|
if page < 1 or page > 1000000:
|
|
raise ValueError
|
|
jobs = connector().jobs(status=status, source=source, limit=50, offset=(page - 1) * 50)
|
|
except ValueError as error:
|
|
abort(400, description=str(error))
|
|
return status, source, page, jobs
|
|
|
|
|
|
@bp.get('/')
|
|
def index():
|
|
status, source, page, jobs = selection()
|
|
return render_template('queues/index.html', title='Queue', jobs=jobs, statuses=STATES, selected_status=status, source=source, page_number=page)
|
|
|
|
|
|
@bp.get('/api/jobs')
|
|
def jobs_api():
|
|
_, _, page, jobs = selection()
|
|
return jsonify(jobs=jobs, page=page)
|
|
|
|
|
|
@bp.get('/jobs/<job_id>')
|
|
def detail(job_id):
|
|
try:
|
|
job = connector().get(job_id)
|
|
except ValueError:
|
|
abort(404)
|
|
return render_template('queues/detail.html', title=job['title'], job=job, logs=connector().logs(job_id))
|
|
|
|
|
|
@bp.post('/jobs/<job_id>/<action>')
|
|
def action(job_id, action):
|
|
if not can('content.write'):
|
|
abort(403)
|
|
if action not in ('cancel', 'retry'):
|
|
abort(404)
|
|
try:
|
|
getattr(connector(), action)(job_id)
|
|
current_app.extensions['state'].audit(g.user['username'], 'queues.' + action, job_id)
|
|
flash('Job ' + ('cancelled.' if action == 'cancel' else 'queued for retry.'), 'success')
|
|
except ValueError as error:
|
|
flash(str(error), 'error')
|
|
return redirect(url_for('plugin_queues.detail', job_id=job_id))
|
|
|
|
|
|
plugin = Plugin(
|
|
'queues', 'Queue', version='0.1.1',
|
|
description='Persistent shared job queue with progress, logs, and an internal plugin connector.',
|
|
blueprint=bp, endpoint='plugin_queues.index',
|
|
services={'queue': lambda: Queue(current_app._get_current_object())},
|
|
)
|