# Copyright © The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.

"""Gunicorn settings for the Debusine server."""

import os

from gunicorn.arbiter import Arbiter
from gunicorn.workers.base import Worker

from debusine.project.settings.pkg_paths import DEBUSINE_DATA_PATH
from debusine.server.prometheus_client.redis import mark_process_dead

bind = f"unix:{DEBUSINE_DATA_PATH}/debusine.sock"
# Recommended by https://gunicorn.org/design/#scaling, but may need tuning
# for individual systems.
workers = os.cpu_count() * 2 + 1
worker_class = "uvicorn.workers.UvicornWorker"
# Recycle workers every so often to control memory leaks.
max_requests = 500


def child_exit(server: Arbiter, worker: Worker) -> None:  # noqa: ARG001, U100
    """Tell the Prometheus client that a worker has died."""
    mark_process_dead(worker.pid)
