From 1389b6b510b33736e873ed0fa4b4589e83af94a4 Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Wed, 6 Nov 2019 14:35:33 +0100
Subject: [PATCH] Change logic of how connected sessions are identified.
---
app/corpora/__init__.py | 3 +-
app/corpora/events.py | 30 ++++++++++++++
app/events.py | 39 +++++++++----------
app/templates/corpora/corpus_analysis.html.j2 | 5 +++
4 files changed, 54 insertions(+), 23 deletions(-)
create mode 100644 app/corpora/events.py
diff --git a/app/corpora/__init__.py b/app/corpora/__init__.py
index 85739bc7..8323ec6a 100644
--- a/app/corpora/__init__.py
+++ b/app/corpora/__init__.py
@@ -2,5 +2,4 @@ from flask import Blueprint
corpora = Blueprint('corpora', __name__)
-
-from . import views
+from . import events, views
diff --git a/app/corpora/events.py b/app/corpora/events.py
new file mode 100644
index 00000000..9a7a5af1
--- /dev/null
+++ b/app/corpora/events.py
@@ -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!')
diff --git a/app/events.py b/app/events.py
index 35474c64..bd628c88 100644
--- a/app/events.py
+++ b/app/events.py
@@ -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!')
diff --git a/app/templates/corpora/corpus_analysis.html.j2 b/app/templates/corpora/corpus_analysis.html.j2
index bd2eb46e..33534038 100644
--- a/app/templates/corpora/corpus_analysis.html.j2
+++ b/app/templates/corpora/corpus_analysis.html.j2
@@ -1,6 +1,11 @@
{% extends "full_width.html.j2" %}
{% block page_content %}
+
+
+