nopaque/app/corpora/events.py

46 lines
1.5 KiB
Python
Raw Normal View History

from app import db, socketio
from app.events import connected_sessions
from app.models import Corpus
from flask import current_app, request
from flask_login import login_required
2019-11-06 14:44:06 +00:00
from .CQiClient.CQiClient import CQiClient
import logging
2019-11-07 12:56:21 +00:00
import time
2019-11-07 13:33:58 +00:00
analysis_clients = {}
@socketio.on('init_corpus_analysis')
@login_required
def init_corpus_analysis(corpus_id):
''' TODO: Check if current_user is allowed to subscribe to this '''
2019-11-07 13:33:58 +00:00
time.sleep(5) # wait for IP of container only for dev
socketio.start_background_task(observe_corpus_analysis_connection,
current_app._get_current_object(),
corpus_id,
request.sid)
@socketio.on('query_event')
def recv_query(message):
logger = logging.getLogger(__name__)
logger.warning(message)
2019-11-07 13:33:58 +00:00
analysis_client = analysis_clients[request.sid]
def observe_corpus_analysis_connection(app, corpus_id, session_id):
logger = logging.getLogger(__name__)
with app.app_context():
2019-11-07 09:55:23 +00:00
corpus = Corpus.query.filter_by(id=corpus_id).first()
2019-11-07 13:33:58 +00:00
logger.warning('MÖÖÖÖP')
analysis_client = CQiClient(host=corpus.analysis_container_ip)
analysis_client.ctrl_connect('opaque', 'opaque')
analysis_clients[session_id] = analysis_client
while session_id in connected_sessions:
2019-11-07 13:33:58 +00:00
logger.warning(analysis_client.ctrl_ping())
socketio.sleep(3)
2019-11-07 13:33:58 +00:00
analysis_client.ctrl_bye()
corpus.status = 'stop analysis'
db.session.commit()