<commit_before>#!/usr/bin/env bash

LOG=
EXIT_ON_ERROR=
RESTART_INTERVAL=3

while test $# -ne 0
do
    case "$1" in
        "-l"|"--log")
                LOG="$2"
                shift
                ;;
        "-i"|"--restart-interval")
                RESTART_INTERVAL="$2"
                shift
                ;;
        "-e"|"--exit-on-error")
                EXIT_ON_ERROR=true
                ;;
        "-h"|"--help")
                echo "Usage:"
                echo "${0} [-l|--log logfile] [-i|--restart-interval secs] [-e|--exit-on-error] script-to-supervise"
                exit 1
                ;;
        *)
                CONSUMER_SCRIPT="$1"
                ;;
    esac
    shift
done

[ -z "$CONSUMER_SCRIPT" ] && {
    echo "ERROR: No script to run specified as an argument" >&2
    exit 2
}

[ ! -e "$CONSUMER_SCRIPT" ] && {
    echo "ERROR: Script \"$CONSUMER_SCRIPT\" not found." >&2
    exit 3
}

[ ! -x "$CONSUMER_SCRIPT" ] && {
    echo "ERROR: script \"$CONSUMER_SCRIPT\" is not executable." >&2
    exit 4
}

until false; do
    if [ "$LOG" ]; then
        "$CONSUMER_SCRIPT" 2>"$LOG" 2>&1
    else
        "$CONSUMER_SCRIPT"
    fi
    exitCode=$?
    [ "$exitCode" != "0" ] && [ $EXIT_ON_ERROR ] && {
        echo "The script \"$CONSUMER_SCRIPT\" died with the error code $exitCode." >&2
        exit 5<commit_msg>Issue #747: Implement clean shutdown on SIGTERM for workers under control of the supervisor script<commit_after>#!/usr/bin/env bash

LOG=
EXIT_ON_ERROR=
RESTART_INTERVAL=3

while test $# -ne 0
do
    case "$1" in
        "-l"|"--log")
                LOG="$2"
                shift
                ;;
        "-i"|"--restart-interval")
                RESTART_INTERVAL="$2"
                shift
                ;;
        "-e"|"--exit-on-error")
                EXIT_ON_ERROR=true
                ;;
        "-h"|"--help")
                echo "Usage:"
                echo "${0} [-l|--log logfile] [-i|--restart-interval secs] [-e|--exit-on-error] script-to-supervise"
                exit 1
                ;;
        *)
                CONSUMER_SCRIPT="$1"
                ;;
    esac
    shift
done

[ -z "$CONSUMER_SCRIPT" ] && {
    echo "ERROR: No script to run specified as an argument" >&2
    exit 2
}

[ ! -e "$CONSUMER_SCRIPT" ] && {
    echo "ERROR: Script \"$CONSUMER_SCRIPT\" not found." >&2
    exit 3
}

[ ! -x "$CONSUMER_SCRIPT" ] && {
    echo "ERROR: script \"$CONSUMER_SCRIPT\" is not executable." >&2
    exit 4
}

function clean_exit()
{
    runmode=0
}

trap clean_exit TERM

runmode=1
until [ $runmode -eq 0 ]; do
    if [ "$LOG" ]; then
        "$CONSUMER_SCRIPT" 2>"$LOG" 2>&1
    else
        "$CONSUMER_SCRIPT"
    fi
    exitCode=$?
    [ "$exitCode" != "0" ] && [ $EXIT_ON_ERROR ] && {
        echo "The script \"$CONSUMER_SCRIPT\" died with the error code $exitCode." >&2
        exit 5
{'fraction_non_alphanumeric': 0.15835367980467388, 'fraction_numerical': 0.016044645971398673, 'mean_word_length': 1.567591763652641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6311605', 'n_tokens_mistral': 1085, 'n_tokens_neox': 975, 'n_words': 232}