#!/bin/bash

# Support directly run the container with runsvdir command.
# MIR will use this command as default command for non-byoc container, and it already handles
# gracefully shutdown from infra level, no need to handle SIGTERM in serving mode.
[[ -z "${PROMPTFLOW_AUTO_DETECT}" ]] && source /service/scripts/auto_detect_env.sh
RUN_MODE=${PROMPTFLOW_RUN_MODE:-"compute"}
DISABLE_MAX_CPU_CORES=4
if [ "$RUN_MODE" = "serving" ]; then
    # only enable this for managed online endpoint
    if [[ -n "${AZUREML_MODEL_DIR}" && $PROMPTFLOW_WORKER_NUM -gt $DISABLE_MAX_CPU_CORES ]]; then
        echo "$(date -uIns) - Start token provider service..."
        gunicorn -w 4 --worker-class uvicorn.workers.UvicornWorker -b 0.0.0.0:8081 --timeout 300 token_provider:app
    else
        # no need to start the token provider service
        sleep infinity
    fi
else
    # no need to start the token provider service
    sleep infinity
fi
