services:
  - type: web
    name: ruralpoint
    env: python
    region: ohio  # us-east-2
    buildCommand: "chmod +x build.sh && ./build.sh"
    # Run migrations before starting the app
    preDeployCommand: "python manage.py migrate --noinput"
    # Start command with worker configuration for better performance
    startCommand: >
      gunicorn ruralpoint_system.wsgi:application 
      --bind 0.0.0.0:$PORT 
      --timeout 120 
      --workers 4 
      --threads 2 
      --worker-class gthread 
      --max-requests 1000 
      --max-requests-jitter 50 
      --log-level info
    envVars:
      - key: PYTHON_VERSION
        value: 3.13.5
      # Memory optimization settings
      - key: PYTHONMALLOC
        value: debug
      - key: MALLOC_TRIM_THRESHOLD_
        value: "65536"
      - key: MPLBACKEND
        value: "Agg"
      - key: OPENBLAS_NUM_THREADS
        value: "1"
      - key: MKL_NUM_THREADS
        value: "1"
      - key: NUMEXPR_NUM_THREADS
        value: "1"
      - key: OMP_NUM_THREADS
        value: "1"
    # Increase instance size to handle data processing
    instanceType: basic
      - key: DATABASE_URL
        value: postgresql://neondb_owner:npg_M0ErNvIbF5aG@ep-old-frost-aemou52p-pooler.c-2.us-east-2.aws.neon.tech/ruralpoint_system?sslmode=require
      - key: DJANGO_SETTINGS_MODULE
        value: ruralpoint_system.settings_production
      - key: SECRET_KEY
        generateValue: true
      - key: WEB_CONCURRENCY
        value: 4
      - key: DJANGO_DEBUG
        value: "False"
    healthCheckPath: /health/
    autoDeploy: true
    buildFilter:
      paths:
        - ruralpoint_system/**
        - loans/**
        - users/**
        - utils/**
        - reports/**
        - templates/**
        - static/**
        - requirements.txt
        - build.sh
        - manage.py
      ignoredPaths:
        - .git/**
        - .env
        - .env.*
        - *.pyc
        - __pycache__/**
        - logs/**
        - media/**
        - staticfiles/**
    disk:
      name: data
      mountPath: /data
      sizeGB: 1