Change corpus analysis socket behavior

This commit is contained in:
Patrick Jentsch 2019-11-18 11:08:33 +01:00
parent fc560933bc
commit 0cd4be055e

View File

@ -26,13 +26,23 @@ def init_corpus_analysis(corpus_id):
if corpus is None: if corpus is None:
socketio.emit('init_corpus_analysis', '[ERROR 404]: Not Found', socketio.emit('init_corpus_analysis', '[ERROR 404]: Not Found',
room=request.sid) room=request.sid)
if not (corpus.creator == current_user or current_user.is_administrator()): elif not (corpus.creator == current_user
or current_user.is_administrator()):
socketio.emit('init_corpus_analysis', '[ERROR 403]: Forbidden', socketio.emit('init_corpus_analysis', '[ERROR 403]: Forbidden',
room=request.sid) room=request.sid)
else:
if corpus_id not in analysis_sessions: if corpus_id not in analysis_sessions:
analysis_sessions[corpus_id] = [request.sid] analysis_sessions[corpus_id] = [request.sid]
else: else:
analysis_sessions[corpus_id].append(request.sid) analysis_sessions[corpus_id].append(request.sid)
while corpus.status != 'analysing':
db.session.refresh(corpus)
socketio.sleep(3)
analysis_clients[request.sid] = CQiClient(
host='{}_analysis_container{}'.format(corpus.creator.username,
corpus.id))
analysis_clients[request.sid].ctrl_connect('opaque', 'opaque')
socketio.emit('init_corpus_analysis', 'Ready', room=request.sid)
socketio.start_background_task(observe_corpus_analysis_connection, socketio.start_background_task(observe_corpus_analysis_connection,
current_app._get_current_object(), current_app._get_current_object(),
corpus_id, request.sid) corpus_id, request.sid)
@ -41,8 +51,11 @@ def init_corpus_analysis(corpus_id):
@socketio.on('query') @socketio.on('query')
@login_required @login_required
def recv_query(message): def recv_query(message):
logger.warning(message) analysis_client = analysis_clients.get(request.sid)
analysis_client = analysis_clients[request.sid] if analysis_client is None:
socketio.emit('query', '[ERROR 424]: Failed Dependency',
room=request.sid)
return
""" Prepare and execute a query """ """ Prepare and execute a query """
corpus = 'CORPUS' corpus = 'CORPUS'
query = message['query'] query = message['query']
@ -85,23 +98,14 @@ def recv_query(message):
def observe_corpus_analysis_connection(app, corpus_id, session_id): def observe_corpus_analysis_connection(app, corpus_id, session_id):
with app.app_context(): with app.app_context():
corpus = Corpus.query.get(corpus_id)
if corpus is None:
''' TODO: Handle this '''
return
while corpus.status != 'analysing':
db.session.refresh(corpus)
socketio.sleep(3)
analysis_client = CQiClient(host='{}_analysis_container{}'.format(corpus.creator.username, corpus.id), port=4877)
analysis_client.ctrl_connect('opaque', 'opaque')
analysis_clients[session_id] = analysis_client
socketio.emit('init_corpus_analysis', 'Ready', room=session_id)
while session_id in connected_sessions: while session_id in connected_sessions:
socketio.sleep(3) socketio.sleep(3)
analysis_client = analysis_clients.pop(session_id, None)
if analysis_client is not None:
analysis_client.ctrl_bye() analysis_client.ctrl_bye()
analysis_clients.pop(session_id, None)
analysis_sessions[corpus_id].remove(session_id) analysis_sessions[corpus_id].remove(session_id)
if not analysis_sessions[corpus_id]: if not analysis_sessions[corpus_id]:
analysis_sessions.pop(corpus_id, None) analysis_sessions.pop(corpus_id, None)
corpus = Corpus.query.get(corpus_id)
corpus.status = 'stop analysis' corpus.status = 'stop analysis'
db.session.commit() db.session.commit()