# linux script framework uwsgi about function encapsulation
# Longwei Lai
###################################################################

source "${____FWK_SCRIPT_PATH}/lnx_fwk_log.sh"
source "${____FWK_SCRIPT_PATH}/lnx_fwk_progress.sh"

____UWSGI_PID_FILE=${WEBSVR_PATH}/.uwsgi.pid
____UWSGI_FIFO_FILE=${WEBSVR_PATH}/.uwsgi_master_fifo
____UWSGI_CFG_FILE="${WEBSVR_PATH}/uwsgi.ini"

____UWSGI_LOG_PATH="${WEBSVR_PATH}/logs"
____UWSGI_LOG_FILE_NAME="${____UWSGI_LOG_PATH}/.webserver_`date "+%m_%d_%H_%M_%S.nohuplog"`"

# makesure log path created
# !!! comment for onemt
# mkdir -p "${____UWSGI_LOG_PATH}"

# start uwsgi
function start_uwsgi()
{
    if [ `is_uwsgi_running` = TRUE ]; then
        log_err "uwsgi running(pid:`get_uwsgi_pid`), please stop first"
        return 1
    fi

    # enter working directory
    local old_pwd="`pwd`"
    if [ "${old_pwd}" != "${WEBSVR_PATH}" ]; then
        cd "${WEBSVR_PATH}"
    fi

    # startup uwsgi
    nohup uwsgi "${____UWSGI_CFG_FILE}" >>"${____UWSGI_LOG_FILE_NAME}" 2>&1 &
    local uwsgi_pid=$!

    # check startup or not
    local ret_code=$?
    if [ ${ret_code} -ne 0 ]; then
        log_err "start uwsgi failed, return code: ${ret_code}"
        cd "${old_pwd}"
        return 1
    fi

    # store uwsgi parent process pid to pid file.
    echo -n ${uwsgi_pid} > "${____UWSGI_PID_FILE}"

    cd "${old_pwd}"
    log_info "uwsgi started, master progress pid: ${uwsgi_pid}"
}

# stop uwsgi
function stop_uwsgi()
{
    if [ `is_uwsgi_running` = FALSE ]; then
        log_err 'uwsgi not running, stop failed!'
        return 1
    fi

    local uwsgi_pid=`get_uwsgi_pid`
    stop_pidfile "${____UWSGI_PID_FILE}"

    log_info "uwsgi(pid:${uwsgi_pid}) stopped!"
}

# 重新载入py代码(在代码有修改后调用,将完成所有workers的优雅重启)
function reload_uwsgi()
{
    reload_uwsgi_check
    if [ $? != 0 ]; then
        return 1
    fi

    # Send SIGHUP to reload.
    # local uwsgi_pid=`get_uwsgi_pid`
    # kill -SIGHUP ${uwsgi_pid}

    # Write 'r' command to fifo file
    echo 'r' > "${____UWSGI_FIFO_FILE}"

    log_info 'all uwsgi workers reloaded!'
}

function chain_reload_uwsgi()
{
    reload_uwsgi_check
    if [ $? != 0 ]; then
        return 1
    fi

    # Write 'c' command to fifo file
    echo 'c' > "${____UWSGI_FIFO_FILE}"

    sleep 6.18
    log_info 'all uwsgi workers reloaded!'
}

function reload_uwsgi_check()
{
    if [ `is_uwsgi_running` = FALSE ]; then
        log_warn "uwsgi not start, will startup first"
        start_uwsgi
        if [ $? != 0 ]; then
            return 1
        fi

        log_info 'wait 5 seconds to wait all uwsgi workers startup done...'
        sleep 5
    fi

    return 0
}

function show_uwsgi_status()
{
    if [ `is_uwsgi_running` = TRUE ]; then
        log_info "uwsgi running, pid:`get_uwsgi_pid`"
        echo 's' > "${____UWSGI_FIFO_FILE}"
    else
        log_info "uwsgi stopped!"
    fi
}

# 确认uwsgi是否在运行中
function is_uwsgi_running()
{
    is_pidfile_running "${____UWSGI_PID_FILE}"
}

# 取得uwsgi pid
function get_uwsgi_pid()
{
    if [ `is_uwsgi_running` != "TRUE" ]; then
        echo -n "0"
    else
        echo -n "`cat "${____UWSGI_PID_FILE}"`"
    fi
}

