<commit_before>#!/usr/bin/env bash
## Author: <NAME>

run() {
    init
    start
}

init() {
    if [ -z "$__INIT" ]; then 
        __INIT="1"
    else
        return 0
    fi
    trap "echo '> script: incomplete termination requested'" TERM   
    set -e
    # script dir
    local dir=$( dirname "${BASH_SOURCE[0]}" )
    # Go to the parent directory of this script's dir
    local pdir="$( cd "${dir}/../" && pwd )"
    pushd . > /dev/null
    cd ${pdir}
    setup_vars
}

setup_vars() {
    PKG_BASE_NAME="$(basename $(pwd))"
    BIN_NAME="${BIN_NAME:-${PKG_BASE_NAME}}"
    LOGS_DIR="./logs"
    # Pointing to a web root, that could be anywhere.
    # This dir is not touched by the deployment.
    WEBROOT_DIR="./www"
    CERT_CACHE_DIR="./cert-cache"
}

start() {
    init
    stop
    echo "> run: start"
    mkdir -p "$LOGS_DIR" "$CERT_CACHE_DIR"
    local binary_path="./${BIN_NAME}"
    sudo setcap cap_net_bind_service=+ep "$binary_path"
    local log_file_exec="${LOGS_DIR}/${PKG_BASE_NAME}-exec.log"
    local log_file="${LOGS_DIR}/${PKG_BASE_NAME}.log"  

    local cmd=$(echo "$binary_path" --address=":443" --root="${WEBROOT_DIR}" --redirector=":80" --dapi-address="localhost:7000" --cert-dir="${CERT_CACHE_DIR}" --log="${log_file}")

    echo "cmd: " $cmd
    nohup $cmd &>> "${log_file_exec}" &

    echo "> run: done"
}

stop() {
    init
    try_exit_or_kill "$BIN_NAME" 90
}

try_exit_or_kill() {
    local pid=$(pidof "$1" || false)
    if [ -z "$pid" ]; then return; fi;
    local d=$(($2*10))
    echo "> run: stopping: $1 (max-wait: ${2}s)"
    local i=0
    while kill "$pid" &>> /dev/null; do
        sleep 0.1s
        i=$((i+1))
        if [ $i -gt $d ]; then 
            echo "> run: sending SIG_KILL: $1"<commit_msg>run script: add wait time to stop<commit_after>#!/usr/bin/env bash
## Author: <NAME>

run() {
    init
    start
}

init() {
    if [ -z "$__INIT" ]; then 
        __INIT="1"
    else
        return 0
    fi
    trap "echo '> script: incomplete termination requested'" TERM   
    set -e
    # script dir
    local dir=$( dirname "${BASH_SOURCE[0]}" )
    # Go to the parent directory of this script's dir
    local pdir="$( cd "${dir}/../" && pwd )"
    pushd . > /dev/null
    cd ${pdir}
    setup_vars
}

setup_vars() {
    PKG_BASE_NAME="$(basename $(pwd))"
    BIN_NAME="${BIN_NAME:-${PKG_BASE_NAME}}"
    LOGS_DIR="./logs"
    # Pointing to a web root, that could be anywhere.
    # This dir is not touched by the deployment.
    WEBROOT_DIR="./www"
    CERT_CACHE_DIR="./cert-cache"
}

start() {
    init
    stop
    echo "> run: start"
    mkdir -p "$LOGS_DIR" "$CERT_CACHE_DIR"
    local binary_path="./${BIN_NAME}"
    sudo setcap cap_net_bind_service=+ep "$binary_path"
    local log_file_exec="${LOGS_DIR}/${PKG_BASE_NAME}-exec.log"
    local log_file="${LOGS_DIR}/${PKG_BASE_NAME}.log"  

    local cmd=$(echo "$binary_path" --address=":443" --root="${WEBROOT_DIR}" --redirector=":80" --dapi-address="localhost:7000" --cert-dir="${CERT_CACHE_DIR}" --log="${log_file}")

    echo "cmd: " $cmd
    nohup $cmd &>> "${log_file_exec}" &

    echo "> run: done"
}

stop() {
    init
    local wait_time=${1:-90}
    try_exit_or_kill "$BIN_NAME" $wait_time
}

try_exit_or_kill() {
    local pid=$(pidof "$1" || false)
    if [ -z "$pid" ]; then return; fi;
    local d=$(($2*10))
    echo "> run: stopping: $1 (max-wait: ${2}s)"
    local i=0
    while kill "$pid" &>> /dev/null; do
        sleep 0.1s
        i=$((i+1))
        if [ $i -gt $d ]; then 
            echo "> run: sending SIG_KILL: $1"
{'fraction_non_alphanumeric': 0.18227848101265823, 'fraction_numerical': 0.014345991561181435, 'mean_word_length': 2.6887966804979255, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2214698', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1327, 'n_words': 321}