mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-24 10:34:17 +00:00
Only use one generic function for socketio background tasks and rename the socketio event (it's no connect so don't name it like one)
This commit is contained in:
parent
9f74e8ce82
commit
d269249db0
@ -5,7 +5,6 @@ from .. import db, socketio
|
||||
from ..models import User
|
||||
import json
|
||||
import jsonpatch
|
||||
import logging
|
||||
|
||||
|
||||
'''
|
||||
@ -24,18 +23,16 @@ def connect():
|
||||
' will be used for further information exchange generated by a background
|
||||
' task associated with the sid.
|
||||
'''
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.warning('[connect] Session id is: {}.'.format(request.sid))
|
||||
socketio.start_background_task(background_task,
|
||||
current_app._get_current_object(),
|
||||
current_user.id,
|
||||
request.sid)
|
||||
|
||||
|
||||
@socketio.on('connect_admin')
|
||||
@socketio.on('inspect_user')
|
||||
@login_required
|
||||
@admin_required
|
||||
def connect_admin(selected_user_id):
|
||||
def inspect_user(user_id):
|
||||
'''
|
||||
' The Socket.IO module creates a session id (sid) on each request. The
|
||||
' initiating admin is automatically placed in a room with that sid, which
|
||||
@ -43,14 +40,11 @@ def connect_admin(selected_user_id):
|
||||
' task associated with the sid. Admin will be placed in that room on emiting
|
||||
' "conncect_admin".
|
||||
'''
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.warning('Admin emitted "connect_admin".')
|
||||
logger.warning('[connect_admin] Session id is: {}.'.format(request.sid))
|
||||
logger.warning('Selected user id is: {}'.format(selected_user_id))
|
||||
socketio.start_background_task(background_task_foreign,
|
||||
socketio.start_background_task(background_task,
|
||||
current_app._get_current_object(),
|
||||
selected_user_id,
|
||||
request.sid)
|
||||
user_id,
|
||||
request.sid,
|
||||
True)
|
||||
|
||||
|
||||
@socketio.on('disconnect')
|
||||
@ -63,7 +57,7 @@ def disconnect():
|
||||
disconnected.append(request.sid)
|
||||
|
||||
|
||||
def background_task(app, user_id, session_id):
|
||||
def background_task(app, user_id, session_id, foreign=False):
|
||||
'''
|
||||
' Sends initial corpus and job lists to the client. Afterwards it checks
|
||||
' every 3 seconds if changes to the initial values appeared. If changes are
|
||||
@ -76,18 +70,14 @@ def background_task(app, user_id, session_id):
|
||||
'''
|
||||
with app.app_context():
|
||||
user = db.session.query(User).filter_by(id=user_id).first()
|
||||
logging.getLogger(__name__)
|
||||
logging.warning('User object is: {}'.format(user))
|
||||
''' Get current values from the database. '''
|
||||
corpora = user.corpora_as_dict()
|
||||
logging.warning('Corpora are: {}'.format(corpora))
|
||||
jobs = user.jobs_as_dict()
|
||||
logging.warning('Jobs are: {}'.format(jobs))
|
||||
''' Send initial values. '''
|
||||
socketio.emit('init-corpora',
|
||||
socketio.emit('init-foreign-corpora' if foreign else 'init-corpora',
|
||||
json.dumps(corpora),
|
||||
room=session_id)
|
||||
socketio.emit('init-jobs',
|
||||
socketio.emit('init-foreign-jobs' if foreign else 'init-jobs',
|
||||
json.dumps(jobs),
|
||||
room=session_id)
|
||||
''' TODO: Implement maximum runtime for this loop. '''
|
||||
@ -100,62 +90,11 @@ def background_task(app, user_id, session_id):
|
||||
jobs_patch = jsonpatch.JsonPatch.from_diff(jobs, new_jobs)
|
||||
''' In case there are patches, send them to the user. '''
|
||||
if corpus_patch:
|
||||
socketio.emit('update-corpora',
|
||||
socketio.emit('update-foreign-corpora' if foreign else 'update-corpora',
|
||||
corpus_patch.to_string(),
|
||||
room=session_id)
|
||||
if jobs_patch:
|
||||
socketio.emit('update-jobs',
|
||||
jobs_patch.to_string(),
|
||||
room=session_id)
|
||||
''' Set new values as references for the next iteration. '''
|
||||
corpora = new_corpora
|
||||
jobs = new_jobs
|
||||
socketio.sleep(3)
|
||||
disconnected.remove(session_id)
|
||||
|
||||
|
||||
def background_task_foreign(app, user_id, session_id):
|
||||
'''
|
||||
' Sends initial corpus and job lists to the client. Afterwards it checks
|
||||
' every 3 seconds if changes to the initial values appeared. If changes are
|
||||
' detected, a RFC 6902 compliant JSON patch gets send.
|
||||
'
|
||||
' NOTE: The initial values are send as a init-* events.
|
||||
' The JSON patches are send as update-* events.
|
||||
'
|
||||
' > where '*' is either 'corpora' or 'jobs'
|
||||
'''
|
||||
with app.app_context():
|
||||
user = db.session.query(User).filter_by(id=user_id).first()
|
||||
logging.getLogger(__name__)
|
||||
logging.warning('User object is: {}'.format(user))
|
||||
''' Get current values from the database. '''
|
||||
corpora = user.corpora_as_dict()
|
||||
logging.warning('Corpora are: {}'.format(corpora))
|
||||
jobs = user.jobs_as_dict()
|
||||
logging.warning('Jobs are: {}'.format(jobs))
|
||||
''' Send initial values. '''
|
||||
socketio.emit('init-foreign-corpora',
|
||||
json.dumps(corpora),
|
||||
room=session_id)
|
||||
socketio.emit('init-foreign-jobs',
|
||||
json.dumps(jobs),
|
||||
room=session_id)
|
||||
''' TODO: Implement maximum runtime for this loop. '''
|
||||
while session_id not in disconnected:
|
||||
''' Get current values from the database '''
|
||||
new_corpora = user.corpora_as_dict()
|
||||
new_jobs = user.jobs_as_dict()
|
||||
''' Compute JSON patches. '''
|
||||
corpus_patch = jsonpatch.JsonPatch.from_diff(corpora, new_corpora)
|
||||
jobs_patch = jsonpatch.JsonPatch.from_diff(jobs, new_jobs)
|
||||
''' In case there are patches, send them to the user. '''
|
||||
if corpus_patch:
|
||||
socketio.emit('update-foreign-corpora',
|
||||
corpus_patch.to_string(),
|
||||
room=session_id)
|
||||
if jobs_patch:
|
||||
socketio.emit('update-foreign-jobs',
|
||||
socketio.emit('update-foreign-jobs' if foreign else 'update-jobs',
|
||||
jobs_patch.to_string(),
|
||||
room=session_id)
|
||||
''' Set new values as references for the next iteration. '''
|
||||
|
@ -35,7 +35,7 @@
|
||||
</div>
|
||||
<script>
|
||||
var selected_user_id = {{selected_user.id|tojson|safe}}
|
||||
socket.emit('connect_admin', selected_user_id);
|
||||
socket.emit('inspect_user', selected_user_id);
|
||||
</script>
|
||||
<div class="col s12 m6">
|
||||
<div id="job-foreign-list">
|
||||
|
Loading…
Reference in New Issue
Block a user