Restore correct working of eveai_chat using gunicorn

This commit is contained in:
Josako
2024-06-04 15:00:23 +02:00
parent 61e1372dc8
commit 303f9b969b
4 changed files with 6 additions and 34 deletions

View File

@@ -10,8 +10,6 @@ from common.utils.cors_utils import create_cors_after_request
from common.utils.celery_utils import make_celery, init_celery from common.utils.celery_utils import make_celery, init_celery
def create_app(config_file=None): def create_app(config_file=None):
app = Flask(__name__) app = Flask(__name__)

View File

@@ -24,8 +24,10 @@ def register_client():
# Validate tenant_id and api_key here (e.g., check against the database) # Validate tenant_id and api_key here (e.g., check against the database)
if validate_tenant(tenant_id, api_key): if validate_tenant(tenant_id, api_key):
access_token = create_access_token(identity={'tenant_id': tenant_id, 'api_key': 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 return jsonify({'token': access_token}), 200
else: else:
current_app.logger.debug(f'Tenant Registration: Invalid tenant_id ({tenant_id}) or api_key ({api_key})')
return jsonify({'message': 'Invalid credentials'}), 401 return jsonify({'message': 'Invalid credentials'}), 401
@@ -33,14 +35,14 @@ def register_client():
@jwt_required() @jwt_required()
def handle_connect(): def handle_connect():
current_tenant = get_jwt_identity() 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') @socketio.on('message', namespace='/chat')
@jwt_required() @jwt_required()
def handle_message(data): def handle_message(data):
current_tenant = get_jwt_identity() 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 # Store interaction in the database
emit('response', {'data': 'Message received'}, broadcast=True) emit('response', {'data': 'Message received'}, broadcast=True)

View File

@@ -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 eveai_chat import create_app
from common.extensions import socketio
import logging
logging.basicConfig(level=logging.DEBUG)
app = create_app() app = create_app()
if __name__ == '__main__': if __name__ == '__main__':
if debug_mode: app.run(debug=True)
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")

View File

@@ -10,8 +10,6 @@ export FLASK_ENV=development # Use 'production' as appropriate
export FLASK_DEBUG=1 # Use 0 for production export FLASK_DEBUG=1 # Use 0 for production
# Start Flask app # Start Flask app
gunicorn --workers 4 --worker-class gevent -b 0.0.0.0:5002 scripts.run_eveai_chat:app & gunicorn -w 4 -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -b 0.0.0.0:5002 scripts.run_eveai_chat:app
wait
deactivate deactivate