mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-24 02:24:20 +00:00
Change logic of how connected sessions are identified.
This commit is contained in:
parent
076ba7356d
commit
1389b6b510
@ -2,5 +2,4 @@ from flask import Blueprint
|
||||
|
||||
corpora = Blueprint('corpora', __name__)
|
||||
|
||||
|
||||
from . import views
|
||||
from . import events, views
|
||||
|
30
app/corpora/events.py
Normal file
30
app/corpora/events.py
Normal file
@ -0,0 +1,30 @@
|
||||
from app import socketio
|
||||
from app.events import connected_sessions
|
||||
from flask import current_app, request
|
||||
from flask_login import login_required
|
||||
import logging
|
||||
|
||||
|
||||
@socketio.on('init_corpus_analysis')
|
||||
@login_required
|
||||
def init_corpus_analysis(corpus_id):
|
||||
''' TODO: Check if current_user is allowed to subscribe to this '''
|
||||
socketio.start_background_task(init_corpus_analysis_,
|
||||
current_app._get_current_object(),
|
||||
corpus_id,
|
||||
request.sid)
|
||||
|
||||
|
||||
@socketio.on('query_event')
|
||||
def recv_query(message):
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.warning(message)
|
||||
|
||||
|
||||
def init_corpus_analysis_(app, corpus_id, session_id):
|
||||
logger = logging.getLogger(__name__)
|
||||
with app.app_context():
|
||||
while session_id in connected_sessions:
|
||||
logger.warning('Run container, run!')
|
||||
socketio.sleep(3)
|
||||
logger.warning('Stop container, stop!')
|
@ -9,10 +9,10 @@ import logging
|
||||
|
||||
|
||||
'''
|
||||
' A list containing session ids of disconnected Socket.IO sessions. It is used
|
||||
' to signal associated background tasks to stop.
|
||||
' A list containing session ids of connected Socket.IO sessions. It is used to
|
||||
' determine runtimes of associated background tasks.
|
||||
'''
|
||||
disconnected = []
|
||||
connected_sessions = []
|
||||
|
||||
|
||||
@socketio.on('connect')
|
||||
@ -24,12 +24,23 @@ def connect():
|
||||
' will be used for further information exchange generated by a background
|
||||
' task associated with the sid.
|
||||
'''
|
||||
connected_sessions.append(request.sid)
|
||||
socketio.start_background_task(background_task,
|
||||
current_app._get_current_object(),
|
||||
current_user.id,
|
||||
request.sid)
|
||||
|
||||
|
||||
@socketio.on('disconnect')
|
||||
@login_required
|
||||
def disconnect():
|
||||
'''
|
||||
' On disconnect the session id (sid) of the connection gets placed in the
|
||||
' disconnected list (see above).
|
||||
'''
|
||||
connected_sessions.remove(request.sid)
|
||||
|
||||
|
||||
@socketio.on('inspect_user')
|
||||
@login_required
|
||||
@admin_required
|
||||
@ -48,22 +59,6 @@ def inspect_user(user_id):
|
||||
True)
|
||||
|
||||
|
||||
@socketio.on('disconnect')
|
||||
@login_required
|
||||
def disconnect():
|
||||
'''
|
||||
' On disconnect the session id (sid) of the connection gets placed in the
|
||||
' disconnected list (see above).
|
||||
'''
|
||||
disconnected.append(request.sid)
|
||||
|
||||
|
||||
@socketio.on('query_event')
|
||||
def recv_query(message):
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.warning(message)
|
||||
|
||||
|
||||
def background_task(app, user_id, session_id, foreign=False):
|
||||
'''
|
||||
' Sends initial corpus and job lists to the client. Afterwards it checks
|
||||
@ -75,6 +70,7 @@ def background_task(app, user_id, session_id, foreign=False):
|
||||
'
|
||||
' > where '*' is either 'corpora' or 'jobs'
|
||||
'''
|
||||
logger = logging.getLogger(__name__)
|
||||
with app.app_context():
|
||||
user = db.session.query(User).get_or_404(user_id)
|
||||
''' Get current values from the database. '''
|
||||
@ -88,7 +84,8 @@ def background_task(app, user_id, session_id, foreign=False):
|
||||
json.dumps(jobs),
|
||||
room=session_id)
|
||||
''' TODO: Implement maximum runtime for this loop. '''
|
||||
while session_id not in disconnected:
|
||||
while session_id in connected_sessions:
|
||||
logger.warning('Running...')
|
||||
''' Get current values from the database '''
|
||||
new_corpora = user.corpora_as_dict()
|
||||
new_jobs = user.jobs_as_dict()
|
||||
@ -108,4 +105,4 @@ def background_task(app, user_id, session_id, foreign=False):
|
||||
corpora = new_corpora
|
||||
jobs = new_jobs
|
||||
socketio.sleep(3)
|
||||
disconnected.remove(session_id)
|
||||
logger.warning('Stoping!')
|
||||
|
@ -1,6 +1,11 @@
|
||||
{% extends "full_width.html.j2" %}
|
||||
|
||||
{% block page_content %}
|
||||
<script>
|
||||
socket.emit('init_corpus_analysis', {{ corpus.id }});
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
<div class="col s12 m3 l3 sticky">
|
||||
<a class="waves-effect waves-light btn" href="{{ url_for('corpora.corpus', corpus_id=corpus_id) }}"><i class="material-icons left">arrow_back</i>Back to corpus overview</a>
|
||||
|
Loading…
Reference in New Issue
Block a user