From 07103ee4e5aa34065c539d2ec8c4d61964cb68f2 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Thu, 29 Jun 2023 13:17:29 +0200 Subject: [PATCH] Fix issues in cqi_over_sio --- app/corpora/cqi_over_sio/__init__.py | 19 +++++++++++-------- app/corpora/cqi_over_sio/cqi.py | 27 +++++++++++++++------------ app/static/js/cqi/models/corpora.js | 6 +++--- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/app/corpora/cqi_over_sio/__init__.py b/app/corpora/cqi_over_sio/__init__.py index 5c2131bd..717f6015 100644 --- a/app/corpora/cqi_over_sio/__init__.py +++ b/app/corpora/cqi_over_sio/__init__.py @@ -84,7 +84,7 @@ def connect(auth): retry_counter -= 1 db.session.refresh(corpus) cqi_client = CQiClient(f'cqpserver_{corpus_id}') - session['d'] = { + session['cqi_over_sio'] = { 'corpus_id': corpus_id, 'cqi_client': cqi_client, 'cqi_client_lock': Lock(), @@ -94,16 +94,19 @@ def connect(auth): @socketio.on('disconnect', namespace=NAMESPACE) def disconnect(): - if 'd' not in session: - return - session['d']['cqi_client_lock'].acquire() try: - session['d']['cqi_client'].api.ctrl_bye() + cqi_client: CQiClient = session['cqi_over_sio']['cqi_client'] + cqi_client_lock: Lock = session['cqi_over_sio']['cqi_client_lock'] + except KeyError: + return + cqi_client_lock.acquire() + try: + cqi_client.api.ctrl_bye() except (BrokenPipeError, CQiException): pass - session['d']['cqi_client_lock'].release() - corpus = Corpus.query.get(session['d']['corpus_id']) + cqi_client_lock.release() + corpus = Corpus.query.get(session['cqi_over_sio']['corpus_id']) corpus.num_analysis_sessions = Corpus.num_analysis_sessions - 1 db.session.commit() - session.pop('d') + session.pop('cqi_over_sio') # return {'code': 200, 'msg': 'OK'} diff --git a/app/corpora/cqi_over_sio/cqi.py b/app/corpora/cqi_over_sio/cqi.py index 9cbdae67..9b26bcbc 100644 --- a/app/corpora/cqi_over_sio/cqi.py +++ b/app/corpora/cqi_over_sio/cqi.py @@ -1,8 +1,9 @@ -from cqi import APIClient +from cqi import CQiClient from cqi.errors import CQiException from cqi.status import CQiStatus from flask import session from inspect import signature +from threading import Lock from typing import Callable, Dict, List from app import socketio from app.decorators import socketio_login_required @@ -60,18 +61,20 @@ CQI_API_FUNCTIONS: List[str] = [ @socketio.on('cqi_client.api', namespace=ns) @socketio_login_required def cqi_over_sio(fn_data): + try: + fn_name: str = fn_data['fn_name'] + if fn_name not in CQI_API_FUNCTIONS: + raise KeyError + except KeyError: + return {'code': 400, 'msg': 'Bad Request'} fn_name: str = fn_data['fn_name'] fn_args: Dict = fn_data.get('fn_args', {}) - print(f'{fn_name}({fn_args})') - if 'd' not in session: - return {'code': 424, 'msg': 'Failed Dependency'} - api_client: APIClient = session['d']['cqi_client'].api - if fn_name not in CQI_API_FUNCTIONS: - return {'code': 400, 'msg': 'Bad Request'} try: - fn: Callable = getattr(api_client, fn_name) - except AttributeError: - return {'code': 400, 'msg': 'Bad Request'} + cqi_client: CQiClient = session['cqi_over_sio']['cqi_client'] + cqi_client_lock: Lock = session['cqi_over_sio']['cqi_client_lock'] + except KeyError: + return {'code': 424, 'msg': 'Failed Dependency'} + fn: Callable = getattr(cqi_client.api, fn_name) for param in signature(fn).parameters.values(): if param.default is param.empty: if param.name not in fn_args: @@ -81,7 +84,7 @@ def cqi_over_sio(fn_data): continue if type(fn_args[param.name]) is not param.annotation: return {'code': 400, 'msg': 'Bad Request'} - session['d']['cqi_client_lock'].acquire() + cqi_client_lock.acquire() try: return_value = fn(**fn_args) except BrokenPipeError: @@ -100,7 +103,7 @@ def cqi_over_sio(fn_data): } } finally: - session['d']['cqi_client_lock'].release() + cqi_client_lock.release() if isinstance(return_value, CQiStatus): payload = { 'code': return_value.code, diff --git a/app/static/js/cqi/models/corpora.js b/app/static/js/cqi/models/corpora.js index 9af467d0..cd57c767 100644 --- a/app/static/js/cqi/models/corpora.js +++ b/app/static/js/cqi/models/corpora.js @@ -40,21 +40,21 @@ cqi.models.corpora.Corpus = class Corpus extends cqi.models.resource.Model { /** * @returns {cqi.models.attributes.AlignmentAttributeCollection} */ - get alignment_attributes() { + get alignmentAttributes() { return new cqi.models.attributes.AlignmentAttributeCollection(this.client, this); } /** * @returns {cqi.models.attributes.PositionalAttributeCollection} */ - get positional_attributes() { + get positionalAttributes() { return new cqi.models.attributes.PositionalAttributeCollection(this.client, this); } /** * @returns {cqi.models.attributes.StructuralAttributeCollection} */ - get structural_attributes() { + get structuralAttributes() { return new cqi.models.attributes.StructuralAttributeCollection(this.client, this); }