From 303f9b969b700db1e5d5f790d7568038fc48c648 Mon Sep 17 00:00:00 2001 From: Josako Date: Tue, 4 Jun 2024 15:00:23 +0200 Subject: [PATCH] Restore correct working of eveai_chat using gunicorn --- eveai_chat/__init__.py | 2 -- eveai_chat/views/chat_views.py | 6 ++++-- scripts/run_eveai_chat.py | 28 +--------------------------- scripts/start_eveai_chat.sh | 4 +--- 4 files changed, 6 insertions(+), 34 deletions(-) diff --git a/eveai_chat/__init__.py b/eveai_chat/__init__.py index fece195..bcd8d04 100644 --- a/eveai_chat/__init__.py +++ b/eveai_chat/__init__.py @@ -10,8 +10,6 @@ from common.utils.cors_utils import create_cors_after_request from common.utils.celery_utils import make_celery, init_celery - - def create_app(config_file=None): app = Flask(__name__) diff --git a/eveai_chat/views/chat_views.py b/eveai_chat/views/chat_views.py index eaf7d63..b587871 100644 --- a/eveai_chat/views/chat_views.py +++ b/eveai_chat/views/chat_views.py @@ -24,8 +24,10 @@ def register_client(): # Validate tenant_id and api_key here (e.g., check against the database) if validate_tenant(tenant_id, api_key): access_token = create_access_token(identity={'tenant_id': tenant_id, 'api_key': api_key}) + current_app.logger.debug(f'Tenant Registration: Tenant {tenant_id} registered successfully') return jsonify({'token': access_token}), 200 else: + current_app.logger.debug(f'Tenant Registration: Invalid tenant_id ({tenant_id}) or api_key ({api_key})') return jsonify({'message': 'Invalid credentials'}), 401 @@ -33,14 +35,14 @@ def register_client(): @jwt_required() def handle_connect(): current_tenant = get_jwt_identity() - print(f'Tenant {current_tenant["tenant_id"]} connected') + current_app.logger.debug(f'Tenant {current_tenant["tenant_id"]} connected') @socketio.on('message', namespace='/chat') @jwt_required() def handle_message(data): current_tenant = get_jwt_identity() - print(f'Tenant {current_tenant["tenant_id"]} sent a message: {data}') + current_app.logger.debug(f'Tenant {current_tenant["tenant_id"]} sent a message: {data}') # Store interaction in the database emit('response', {'data': 'Message received'}, broadcast=True) diff --git a/scripts/run_eveai_chat.py b/scripts/run_eveai_chat.py index dab01c7..8d6a042 100644 --- a/scripts/run_eveai_chat.py +++ b/scripts/run_eveai_chat.py @@ -1,32 +1,6 @@ -import os - -# Determine if we are in debug mode -debug_mode = os.environ.get('CHAT_DEBUG', 'True').lower() == 'true' - -# Only monkey patch if not in debug mode -if not debug_mode: - from gevent import monkey - monkey.patch_all() - from eveai_chat import create_app -from common.extensions import socketio -import logging - -logging.basicConfig(level=logging.DEBUG) app = create_app() if __name__ == '__main__': - if debug_mode: - logging.info("Starting Flask application in debug mode") - app.config['DEBUG'] = True # Enable debug mode in Flask - app.config['ENV'] = 'development' - socketio.run(app, debug=True, host='0.0.0.0', port=5002, allow_unsafe_werkzeug=True) # Use Flask's built-in server for debugging - else: - logging.info("Starting Flask application with gevent WSGI server") - from gevent.pywsgi import WSGIServer - from geventwebsocket.handler import WebSocketHandler - http_server = WSGIServer(('0.0.0.0', 5002), app, handler_class=WebSocketHandler) - http_server.serve_forever() - -logging.info("Application started") + app.run(debug=True) \ No newline at end of file diff --git a/scripts/start_eveai_chat.sh b/scripts/start_eveai_chat.sh index cee3d31..53dbd18 100755 --- a/scripts/start_eveai_chat.sh +++ b/scripts/start_eveai_chat.sh @@ -10,8 +10,6 @@ export FLASK_ENV=development # Use 'production' as appropriate export FLASK_DEBUG=1 # Use 0 for production # Start Flask app -gunicorn --workers 4 --worker-class gevent -b 0.0.0.0:5002 scripts.run_eveai_chat:app & - -wait +gunicorn -w 4 -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -b 0.0.0.0:5002 scripts.run_eveai_chat:app deactivate \ No newline at end of file