Fix issues in cqi_over_sio

This commit is contained in:
Patrick Jentsch 2023-06-29 13:17:29 +02:00
parent efa8712cd9
commit 07103ee4e5
3 changed files with 29 additions and 23 deletions

View File

@ -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'}

View File

@ -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,

View File

@ -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);
}