Commit 74727405 authored by Julian Rother's avatar Julian Rother
Browse files

Moved {job,edit}_handler functions back into the corresponding modules

parent 4003c9b7
...@@ -210,3 +210,15 @@ def set_responsible(course_id, user_id, value): ...@@ -210,3 +210,15 @@ def set_responsible(course_id, user_id, value):
modify('DELETE FROM responsible WHERE course_id = ? AND user_id = ?', course_id, user_id); modify('DELETE FROM responsible WHERE course_id = ? AND user_id = ?', course_id, user_id);
return "OK", 200 return "OK", 200
edit_handlers = {}
def edit_handler(*tables, field=None):
def wrapper(func):
for table in tables:
if table not in edit_handlers:
edit_handlers[table] = {}
if field not in edit_handlers[table]:
edit_handlers[table][field] = []
edit_handlers[table][field].append(func)
return func
return wrapper
...@@ -2,7 +2,24 @@ from server import * ...@@ -2,7 +2,24 @@ from server import *
import traceback import traceback
import json import json
import random import random
from sorter import schedule_thumbnail
job_handlers = {}
def job_handler(*types, state='finished'):
def wrapper(func):
for jobtype in types:
if jobtype not in job_handlers:
job_handlers[jobtype] = {}
if state not in job_handlers[jobtype]:
job_handlers[jobtype][state] = []
job_handlers[jobtype][state].append(func)
return func
return wrapper
def schedule_job(jobtype, data=None, priority=0, queue="default"):
if not data:
data = {}
modify('INSERT INTO jobs (type, priority, queue, data, time_created) VALUES (?, ?, ?, ?, ?)',
jobtype, priority, queue, json.dumps(data, default=date_json_handler), datetime.now())
@app.route('/internal/jobs/overview') @app.route('/internal/jobs/overview')
@register_navbar('Jobs', iconlib='fa', icon='suitcase') @register_navbar('Jobs', iconlib='fa', icon='suitcase')
...@@ -42,19 +59,14 @@ def jobs_overview(): ...@@ -42,19 +59,14 @@ def jobs_overview():
def jobs_action(action, jobid=None): def jobs_action(action, jobid=None):
if action == 'clear_failed': if action == 'clear_failed':
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)
if 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)
if action == 'copy': elif action == 'copy':
if 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)
if action == 'delete': elif action == 'delete':
if jobid: if jobid:
query('UPDATE jobs SET state="deleted" WHERE id = ?', jobid) query('UPDATE jobs SET state="deleted" WHERE id = ?', jobid)
if action == 'add':
jobtype = request.values.get('type', None)
if jobtype == 'thumbnail':
lectureid = int(request.values.get('lecture_id', -1))
schedule_thumbnail(lectureid)
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):
......
...@@ -3,9 +3,8 @@ from server import * ...@@ -3,9 +3,8 @@ from server import *
@sched_func(30) @sched_func(30)
def livestream_thumbnail(): def livestream_thumbnail():
livestreams = query('SELECT streams.lecture_id, streams.handle AS livehandle FROM streams WHERE streams.active') livestreams = query('SELECT streams.lecture_id, streams.handle AS livehandle FROM streams WHERE streams.active')
for v in genlive(livestreams): for v in genlive(livestreams):
sorter.schedule_thumbnail(v['lecture_id'], v['path']) schedule_job('thumbnail', {'lectureid': v['lecture_id'], 'path': v['path']})
@app.route('/internal/streaming/legacy_auth', methods=['GET', 'POST']) @app.route('/internal/streaming/legacy_auth', methods=['GET', 'POST'])
def streamauth(): def streamauth():
......
...@@ -483,47 +483,17 @@ def dbstatus(): ...@@ -483,47 +483,17 @@ def dbstatus():
clusters[cluster].append(host) clusters[cluster].append(host)
return render_template('dbstatus.html', clusters=clusters, statuses=status, vars=variables), 200 return render_template('dbstatus.html', clusters=clusters, statuses=status, vars=variables), 200
job_handlers = {}
def job_handler(*types, state='finished'):
def wrapper(func):
for jobtype in types:
if jobtype not in job_handlers:
job_handlers[jobtype] = {}
if state not in job_handlers[jobtype]:
job_handlers[jobtype][state] = []
job_handlers[jobtype][state].append(func)
return func
return wrapper
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
def schedule_job(jobtype, data=None, priority=0, queue="default"): from jobs import job_handler, schedule_job
if not data: from edit import edit_handler
data = {}
modify('INSERT INTO jobs (type, priority, queue, data, time_created) VALUES (?, ?, ?, ?, ?)',
jobtype, priority, queue, json.dumps(data, default=date_json_handler), datetime.now())
edit_handlers = {}
def edit_handler(*tables, field=None):
def wrapper(func):
for table in tables:
if table not in edit_handlers:
edit_handlers[table] = {}
if field not in edit_handlers[table]:
edit_handlers[table][field] = []
edit_handlers[table][field].append(func)
return func
return wrapper
import edit
import feeds import feeds
import importer import importer
import stats import stats
if 'ICAL_URL' in config: if 'ICAL_URL' in config:
import meetings import meetings
import l2pauth import l2pauth
import jobs
import sorter import sorter
import encoding import encoding
import timetable import timetable
......
...@@ -49,21 +49,22 @@ def insert_video(lectureid, dbfilepath, fileformatid, hash="", filesize=-1): ...@@ -49,21 +49,22 @@ def insert_video(lectureid, dbfilepath, fileformatid, hash="", filesize=-1):
schedule_thumbnail(lectureid) schedule_thumbnail(lectureid)
schedule_job('probe', {'path': dbfilepath, 'lecture_id': lectureid, 'video_id': video_id, 'import-chapters': True}) schedule_job('probe', {'path': dbfilepath, 'lecture_id': lectureid, 'video_id': video_id, 'import-chapters': True})
def schedule_thumbnail(lectureid, filePath=None): @app.route('/internal/jobs/add/thumbnail', methods=['GET', 'POST'])
@mod_required
@handle_errors('jobs_overview', 'Zu dieser Veranstaltung existieren keine Videos!', 404, IndexError)
def schedule_thumbnail(lectureid=None):
ret = None
if not lectureid:
lectureid = request.values['lectureid']
ret = redirect(request.values.get('ref', url_for('jobs_overview')))
videos = query(''' videos = query('''
SELECT videos.path SELECT videos.path
FROM videos FROM videos
JOIN formats ON (videos.video_format = formats.id) JOIN formats ON (videos.video_format = formats.id)
WHERE videos.lecture_id = ? WHERE videos.lecture_id = ?
ORDER BY formats.prio DESC''', lectureid ) ORDER BY formats.prio DESC''', lectureid)
if videos: schedule_job('thumbnail', {'lectureid': lectureid, 'path': videos[0]['path']})
path = videos[0]['path'] return ret
elif filePath:
path = filePath
else:
return
data = '{"lectureid": "'+str(lectureid)+'", "path": "'+path+'"}'
query('INSERT INTO jobs (type, data, time_created) VALUES ("thumbnail", ?, ?)', data, datetime.now());
@job_handler('transcode') @job_handler('transcode')
def insert_transcoded_video(jobid, jobtype, data, state, status): def insert_transcoded_video(jobid, jobtype, data, state, status):
......
from server import * from server import *
import json import json
from jobs import date_json_handler
from hashlib import md5 from hashlib import md5
from datetime import datetime from datetime import datetime
......
...@@ -17,10 +17,9 @@ ...@@ -17,10 +17,9 @@
<div class="tab-content" style="margin-top: 10px;"> <div class="tab-content" style="margin-top: 10px;">
<div role="tabpanel" class="tab-pane active" id="add_thumbnail"> <div role="tabpanel" class="tab-pane active" id="add_thumbnail">
<form class="form-inline" action="{{url_for('jobs_action', action='add', ref=request.url)}}" method="post"> <form class="form-inline" action="{{url_for('schedule_thumbnail', ref=request.url)}}" method="post">
<div class="form-group"> <div class="form-group">
<input type="hidden" name="type" value="thumbnail"> <input type="text" class="form-control" placeholder="Lecture ID" name="lectureid">
<input type="text" class="form-control" id="thumbnail_lectureid" placeholder="Lecture ID" name="lecture_id">
<button type="submit" class="btn btn-primary">Hinzufügen</button> <button type="submit" class="btn btn-primary">Hinzufügen</button>
</div> </div>
</form> </form>
......
Supports Markdown
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