"""Gunicorn configuration for DataOps Platform production."""

import os

bind = f"{os.environ.get('LISTEN_HOST', '0.0.0.0')}:{os.environ.get('LISTEN_PORT', '5500')}"
workers = int(os.environ.get("GUNICORN_WORKERS", "4"))
timeout = int(os.environ.get("GUNICORN_TIMEOUT", "120"))
capture_output = True
enable_stdio_inheritance = True
loglevel = "info"


def post_worker_init(worker):
    """Re-apply Flask file logging after Gunicorn worker initialization."""
    del worker
    try:
        from wsgi import application

        from app import configure_logging

        configure_logging(application)
    except Exception as exc:
        import sys

        print(f"[gunicorn] post_worker_init logging setup failed: {exc}", file=sys.stderr)


def worker_exit(server, worker):
    """Dispose process-local external data-source pools on Worker exit."""
    del server, worker
    from app.core.data_source.runtime import close_data_source_runtime

    close_data_source_runtime()
