Change logic of how connected sessions are identified.

This commit is contained in:
Patrick Jentsch 2019-11-06 14:35:33 +01:00
parent 076ba7356d
commit 1389b6b510
4 changed files with 54 additions and 23 deletions

View File

@ -2,5 +2,4 @@ from flask import Blueprint
corpora = Blueprint('corpora', __name__) corpora = Blueprint('corpora', __name__)
from . import events, views
from . import views

30
app/corpora/events.py Normal file
View 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!')

View File

@ -9,10 +9,10 @@ import logging
''' '''
' A list containing session ids of disconnected Socket.IO sessions. It is used ' A list containing session ids of connected Socket.IO sessions. It is used to
' to signal associated background tasks to stop. ' determine runtimes of associated background tasks.
''' '''
disconnected = [] connected_sessions = []
@socketio.on('connect') @socketio.on('connect')
@ -24,12 +24,23 @@ def connect():
' will be used for further information exchange generated by a background ' will be used for further information exchange generated by a background
' task associated with the sid. ' task associated with the sid.
''' '''
connected_sessions.append(request.sid)
socketio.start_background_task(background_task, socketio.start_background_task(background_task,
current_app._get_current_object(), current_app._get_current_object(),
current_user.id, current_user.id,
request.sid) 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') @socketio.on('inspect_user')
@login_required @login_required
@admin_required @admin_required
@ -48,22 +59,6 @@ def inspect_user(user_id):
True) 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): def background_task(app, user_id, session_id, foreign=False):
''' '''
' Sends initial corpus and job lists to the client. Afterwards it checks ' 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' ' > where '*' is either 'corpora' or 'jobs'
''' '''
logger = logging.getLogger(__name__)
with app.app_context(): with app.app_context():
user = db.session.query(User).get_or_404(user_id) user = db.session.query(User).get_or_404(user_id)
''' Get current values from the database. ''' ''' Get current values from the database. '''
@ -88,7 +84,8 @@ def background_task(app, user_id, session_id, foreign=False):
json.dumps(jobs), json.dumps(jobs),
room=session_id) room=session_id)
''' TODO: Implement maximum runtime for this loop. ''' ''' 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 ''' ''' Get current values from the database '''
new_corpora = user.corpora_as_dict() new_corpora = user.corpora_as_dict()
new_jobs = user.jobs_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 corpora = new_corpora
jobs = new_jobs jobs = new_jobs
socketio.sleep(3) socketio.sleep(3)
disconnected.remove(session_id) logger.warning('Stoping!')

View File

@ -1,6 +1,11 @@
{% extends "full_width.html.j2" %} {% extends "full_width.html.j2" %}
{% block page_content %} {% block page_content %}
<script>
socket.emit('init_corpus_analysis', {{ corpus.id }});
</script>
<div class="col s12 m3 l3 sticky"> <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> <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>