From 67bdeac43476db4eba1e1e2a8d4a7ce07f74f65d Mon Sep 17 00:00:00 2001 From: Josako Date: Mon, 16 Sep 2024 16:17:54 +0200 Subject: [PATCH] - Improvements and bugfixes to HealthChecks --- CHANGELOG.md | 13 +++++++++++++ common/utils/cors_utils.py | 8 ++++++++ common/utils/view_assistants.py | 5 +++-- docker/compose_stackhero.yaml | 32 ++++--------------------------- docker/eveai_api/Dockerfile | 1 + docker/eveai_app/Dockerfile | 1 + docker/eveai_chat/Dockerfile | 1 + eveai_api/__init__.py | 30 ++++++++++++++++------------- eveai_api/views/healthz_views.py | 2 +- eveai_app/views/user_forms.py | 2 +- eveai_app/views/user_views.py | 2 ++ eveai_chat/__init__.py | 1 - eveai_chat/views/healthz_views.py | 2 +- 13 files changed, 53 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c483b21..4dead88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security - In case of vulnerabilities. +## [1.0.8-alfa] - 2024-09-12 + +### Added +- Tenant type defined to allow for active, inactive, demo ... tenants +- Search and filtering functionality on Tenants +- Implementation of health checks (1st version) +- Provision for Prometheus monitoring (no implementation yet) +- Refine audio_processor and srt_processor to reduce duplicate code and support larger files +- Introduction of repopack to reason in LLMs about the code + +### Fixed +- Refine audio_processor and srt_processor to reduce duplicate code and support larger files + ## [1.0.7-alfa] - 2024-09-12 ### Added diff --git a/common/utils/cors_utils.py b/common/utils/cors_utils.py index 77304c4..534a8ba 100644 --- a/common/utils/cors_utils.py +++ b/common/utils/cors_utils.py @@ -23,6 +23,14 @@ def cors_after_request(response, prefix): current_app.logger.debug(f'request.args: {request.args}') current_app.logger.debug(f'request is json?: {request.is_json}') + # Exclude health checks from checks + if request.path.startswith('/healthz') or request.path.startswith('/_healthz'): + current_app.logger.debug('Skipping CORS headers for health checks') + response.headers.add('Access-Control-Allow-Origin', '*') + response.headers.add('Access-Control-Allow-Headers', '*') + response.headers.add('Access-Control-Allow-Methods', '*') + return response + tenant_id = None allowed_origins = [] diff --git a/common/utils/view_assistants.py b/common/utils/view_assistants.py index 3d94a8f..9d417b0 100644 --- a/common/utils/view_assistants.py +++ b/common/utils/view_assistants.py @@ -1,4 +1,4 @@ -from flask import flash +from flask import flash, current_app def prepare_table(model_objects, column_names): @@ -44,7 +44,8 @@ def form_validation_failed(request, form): for fieldName, errorMessages in form.errors.items(): for err in errorMessages: flash(f"Error in {fieldName}: {err}", 'danger') + current_app.logger.debug(f"Error in {fieldName}: {err}", 'danger') def form_to_dict(form): - return {field.name: field.data for field in form if field.name != 'csrf_token' and hasattr(field, 'data')} \ No newline at end of file + return {field.name: field.data for field in form if field.name != 'csrf_token' and hasattr(field, 'data')} diff --git a/docker/compose_stackhero.yaml b/docker/compose_stackhero.yaml index a7872f0..9a6734b 100644 --- a/docker/compose_stackhero.yaml +++ b/docker/compose_stackhero.yaml @@ -53,10 +53,6 @@ services: environment: <<: *common-variables volumes: -# - ../nginx:/etc/nginx -# - ../nginx/sites-enabled:/etc/nginx/sites-enabled -# - ../nginx/static:/etc/nginx/static -# - ../nginx/public:/etc/nginx/public - eveai_logs:/var/log/nginx labels: - "traefik.enable=true" @@ -81,7 +77,7 @@ services: volumes: - eveai_logs:/app/logs healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:5001/health"] + test: ["CMD", "curl", "-f", "http://localhost:5001/healthz/ready"] interval: 10s timeout: 5s retries: 5 @@ -91,18 +87,11 @@ services: eveai_workers: platform: linux/amd64 image: josakola/eveai_workers:latest -# ports: -# - 5001:5001 environment: <<: *common-variables COMPONENT_NAME: eveai_workers volumes: - eveai_logs:/app/logs -# healthcheck: -# test: [ "CMD", "curl", "-f", "http://localhost:5001/health" ] -# interval: 10s -# timeout: 5s -# retries: 5 networks: - eveai-network @@ -117,7 +106,7 @@ services: volumes: - eveai_logs:/app/logs healthcheck: - test: [ "CMD", "curl", "-f", "http://localhost:5002/health" ] # Adjust based on your health endpoint + test: [ "CMD", "curl", "-f", "http://localhost:5002/healthz/ready" ] # Adjust based on your health endpoint interval: 10s timeout: 5s retries: 5 @@ -127,18 +116,11 @@ services: eveai_chat_workers: platform: linux/amd64 image: josakola/eveai_chat_workers:latest -# ports: -# - 5001:5001 environment: <<: *common-variables COMPONENT_NAME: eveai_chat_workers volumes: - eveai_logs:/app/logs -# healthcheck: -# test: [ "CMD", "curl", "-f", "http://localhost:5001/health" ] -# interval: 10s -# timeout: 5s -# retries: 5 networks: - eveai-network @@ -153,7 +135,7 @@ services: volumes: - eveai_logs:/app/logs healthcheck: - test: [ "CMD", "curl", "-f", "http://localhost:5001/health" ] + test: [ "CMD", "curl", "-f", "http://localhost:5003/healthz/ready" ] interval: 10s timeout: 5s retries: 5 @@ -162,11 +144,5 @@ services: volumes: eveai_logs: -# miniAre theo_data: -# db-data: -# redis-data: -# tenant-files: -#secrets: -# db-password: -# file: ./db/password.txt + diff --git a/docker/eveai_api/Dockerfile b/docker/eveai_api/Dockerfile index 7911d88..328638d 100644 --- a/docker/eveai_api/Dockerfile +++ b/docker/eveai_api/Dockerfile @@ -34,6 +34,7 @@ RUN apt-get update && apt-get install -y \ build-essential \ gcc \ postgresql-client \ + curl \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* diff --git a/docker/eveai_app/Dockerfile b/docker/eveai_app/Dockerfile index c7fc9dd..f3064c0 100644 --- a/docker/eveai_app/Dockerfile +++ b/docker/eveai_app/Dockerfile @@ -34,6 +34,7 @@ RUN apt-get update && apt-get install -y \ build-essential \ gcc \ postgresql-client \ + curl \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* diff --git a/docker/eveai_chat/Dockerfile b/docker/eveai_chat/Dockerfile index 4ab8292..74da31b 100644 --- a/docker/eveai_chat/Dockerfile +++ b/docker/eveai_chat/Dockerfile @@ -34,6 +34,7 @@ RUN apt-get update && apt-get install -y \ build-essential \ gcc \ postgresql-client \ + curl \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* diff --git a/eveai_api/__init__.py b/eveai_api/__init__.py index 1214eda..9f48b93 100644 --- a/eveai_api/__init__.py +++ b/eveai_api/__init__.py @@ -76,20 +76,24 @@ def create_app(config_file=None): app.logger.debug('Token request detected, skipping JWT verification') return - try: - verify_jwt_in_request(optional=True) - tenant_id = get_jwt_identity() - app.logger.debug(f'Tenant ID from JWT: {tenant_id}') + # Check if this a health check request + if request.path.startswith('/_healthz') or request.path.startswith('/healthz'): + app.logger.debug('Health check request detected, skipping JWT verification') + else: + try: + verify_jwt_in_request(optional=True) + tenant_id = get_jwt_identity() + app.logger.debug(f'Tenant ID from JWT: {tenant_id}') - if tenant_id: - Database(tenant_id).switch_schema() - app.logger.debug(f'Switched to schema for tenant {tenant_id}') - else: - app.logger.debug('No tenant ID found in JWT') - except Exception as e: - app.logger.error(f'Error in before_request: {str(e)}') - # Don't raise the exception here, let the request continue - # The appropriate error handling will be done in the specific endpoints + if tenant_id: + Database(tenant_id).switch_schema() + app.logger.debug(f'Switched to schema for tenant {tenant_id}') + else: + app.logger.debug('No tenant ID found in JWT') + except Exception as e: + app.logger.error(f'Error in before_request: {str(e)}') + # Don't raise the exception here, let the request continue + # The appropriate error handling will be done in the specific endpoints return app diff --git a/eveai_api/views/healthz_views.py b/eveai_api/views/healthz_views.py index 3d25e3a..b34c3f9 100644 --- a/eveai_api/views/healthz_views.py +++ b/eveai_api/views/healthz_views.py @@ -24,7 +24,7 @@ def liveness(): def readiness(): checks = { "database": check_database(), - "celery": check_celery(), + # "celery": check_celery(), "minio": check_minio(), # Add more checks as needed } diff --git a/eveai_app/views/user_forms.py b/eveai_app/views/user_forms.py index ce31c00..c728443 100644 --- a/eveai_app/views/user_forms.py +++ b/eveai_app/views/user_forms.py @@ -67,7 +67,7 @@ class TenantForm(FlaskForm): # Initialize fallback algorithms self.fallback_algorithms.choices = \ [(algorithm, algorithm.lower()) for algorithm in current_app.config['FALLBACK_ALGORITHMS']] - self.type.choices = [('', 'Select Type')] + [(t, t) for t in current_app.config['TENANT_TYPES']] + self.type.choices = [(t, t) for t in current_app.config['TENANT_TYPES']] class BaseUserForm(FlaskForm): diff --git a/eveai_app/views/user_views.py b/eveai_app/views/user_views.py index ea2ab61..d994393 100644 --- a/eveai_app/views/user_views.py +++ b/eveai_app/views/user_views.py @@ -129,6 +129,7 @@ def edit_tenant(tenant_id): form.html_excluded_classes.data = ', '.join(tenant.html_excluded_classes) if form.validate_on_submit(): + current_app.logger.debug(f'Updating tenant {tenant_id}') # Populate the tenant with form data form.populate_obj(tenant) # Then handle the special fields manually @@ -148,6 +149,7 @@ def edit_tenant(tenant_id): session['tenant'] = tenant.to_dict() # return redirect(url_for(f"user/tenant/tenant_id")) else: + current_app.logger.debug(f'Tenant update failed with errors: {form.errors}') form_validation_failed(request, form) return render_template('user/edit_tenant.html', form=form, tenant_id=tenant_id) diff --git a/eveai_chat/__init__.py b/eveai_chat/__init__.py index 07a8b76..0ce50e5 100644 --- a/eveai_chat/__init__.py +++ b/eveai_chat/__init__.py @@ -60,7 +60,6 @@ def register_extensions(app): session.init_app(app) - def register_blueprints(app): from views.healthz_views import healthz_bp app.register_blueprint(healthz_bp) diff --git a/eveai_chat/views/healthz_views.py b/eveai_chat/views/healthz_views.py index bf4f969..5babd5f 100644 --- a/eveai_chat/views/healthz_views.py +++ b/eveai_chat/views/healthz_views.py @@ -20,7 +20,7 @@ def liveness(): def readiness(): checks = { "database": check_database(), - "celery": check_celery(), + # "celery": check_celery(), # Add more checks as needed }