# Airflow Docker Compose Configuration
# Part of the TextIQ Airflow Migration
# Runs alongside existing docker-compose.yml services

x-airflow-env: &airflow-env
  AIRFLOW__CORE__EXECUTOR: LocalExecutor
  AIRFLOW__DATABASE__SQL_ALCHEMY_CONN: postgresql+psycopg2://${POSTGRES_USER:-textiq}:${POSTGRES_PASSWORD:-textiq_dev_password}@postgres:5432/airflow
  AIRFLOW__CORE__FERNET_KEY: ''
  AIRFLOW__WEBSERVER__SECRET_KEY: ${AIRFLOW_SECRET_KEY:-changeme}
  # Override DATABASE_URL to use Docker network hostname (localhost won't work inside containers)
  # AWS credentials for SeaweedFS S3 access
  AWS_ACCESS_KEY_ID: ${SEAWEEDFS_ACCESS_KEY_ID:-dummy}
  AWS_SECRET_ACCESS_KEY: ${SEAWEEDFS_SECRET_ACCESS_KEY:-dummy}
  AWS_ENDPOINT_URL: http://${SEAWEEDFS_HOST:-seaweedfs}:${SEAWEEDFS_PORT:-8333}
  AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION: 'false'
  AIRFLOW__CORE__LOAD_EXAMPLES: 'false'
  AIRFLOW__CORE__AUTH_MANAGER: airflow.api_fastapi.auth.managers.simple.simple_auth_manager.SimpleAuthManager
  AIRFLOW__CORE__SIMPLE_AUTH_MANAGER_ALL_ADMINS: 'True'
  AIRFLOW__API__AUTH_BACKENDS: 'airflow.api.auth.backend.default'
  AIRFLOW__SCHEDULER__ENABLE_HEALTH_CHECK: 'true'
  AIRFLOW__WEBSERVER__EXPOSE_CONFIG: 'true'
  AIRFLOW__WEBSERVER__RBAC: 'false'
  # Airflow 3.x: Execution API server URL for task SDK communication
  AIRFLOW__CORE__EXECUTION_API_SERVER_URL: 'http://airflow-webserver:8080/execution/'
  # Increase execution API timeout for long-running external_python tasks (default: 30s)
  AIRFLOW__WORKERS__EXECUTION_API_TIMEOUT: '300'
  # Increase task heartbeat timeout for long-running tasks
  # TASK_INSTANCE_HEARTBEAT_SEC: How often task sends heartbeat (default: 5s)
  AIRFLOW__SCHEDULER__TASK_INSTANCE_HEARTBEAT_SEC: '30'
  # TASK_INSTANCE_HEARTBEAT_TIMEOUT: Number of missed heartbeats before marking as failed (default: 60)
  # With 30s heartbeat interval, 120 * 30 = 3600s = 1 hour timeout
  AIRFLOW__SCHEDULER__TASK_INSTANCE_HEARTBEAT_TIMEOUT: '120'
  # Python path for src module access
  # Single worker required so plugin /metrics uses one in-process registry
  AIRFLOW__API__WORKERS: '1'

x-airflow-common: &airflow-common
  build:
    context: ..
    dockerfile: docker/Dockerfile.airflow
  image: textiq-airflow:3.0.6-python3.11
  env_file:
    - ../.env
  environment:
    <<: *airflow-env
  entrypoint: /entrypoint.sh
  user: "${AIRFLOW_UID:-50000}:0"
  # Note: postgres and redis must be running before starting Airflow
  # Start with: docker compose -f docker-compose.yml up -d postgres redis
  networks:
    - textiq-network

services:
  airflow-init:
    <<: *airflow-common
    container_name: textiq-airflow-init
    entrypoint: /bin/bash
    command:
      - -c
      - |
        mkdir -p /opt/airflow/{dags,logs,plugins,config}
        chown -R "${AIRFLOW_UID:-50000}:0" /opt/airflow/{dags,logs,plugins,config}
        exec airflow db migrate
    environment:
      <<: *airflow-env
      _AIRFLOW_DB_MIGRATE: 'true'
    user: "0:0"
    profiles:
      - init

  airflow-webserver:
    <<: *airflow-common
    container_name: textiq-airflow-webserver
    command: webserver
    ports:
      - "8081:8080"
    environment:
      <<: *airflow-env
    healthcheck:
      # Airflow 3.x api-server health endpoint
      test: ["CMD", "curl", "--fail", "http://localhost:8080/api/v2/monitor/health"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    restart: unless-stopped

  airflow-scheduler:
    <<: *airflow-common
    container_name: textiq-airflow-scheduler
    command: scheduler
    healthcheck:
      test: ["CMD-SHELL", "airflow jobs check --job-type SchedulerJob --hostname \"$${HOSTNAME}\""]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s
    restart: unless-stopped

  airflow-dag-processor:
    <<: *airflow-common
    container_name: textiq-airflow-dag-processor
    command: dag-processor
    healthcheck:
      test: ["CMD-SHELL", 'airflow jobs check --job-type DagProcessorJob --hostname "$${HOSTNAME}"']
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s
    restart: unless-stopped

  airflow-triggerer:
    <<: *airflow-common
    container_name: textiq-airflow-triggerer
    command: triggerer
    healthcheck:
      test: ["CMD-SHELL", 'airflow jobs check --job-type TriggererJob --hostname "$${HOSTNAME}"']
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s
    restart: unless-stopped

  airflow-cli:
    <<: *airflow-common
    container_name: textiq-airflow-cli
    profiles:
      - cli
    environment:
      <<: *airflow-env
      CONNECTION_CHECK_MAX_COUNT: "0"
    command:
      - bash
      - -c
      - airflow

networks:
  textiq-network:
    external: true
    name: docker_textiq-network
