#!/bin/bash

##################################################
# Mount dir                                      #
# - /opt/datax/log                               #
# - /opt/datax/log_perf                          #
# - /opt/datax/job                               #
# ENV                                            #
# - JAVA_OPTS                                    #
# - TIMEOUT                                      #
# - MAX_PROCS                                    #
##################################################

set -euo pipefail
export LANG=en_US.UTF-8
trap Quit EXIT

GOT_SIGTERM=
TIMEOUT="${TIMEOUT:-10m}"
MAX_PROCS=${MAX_PROCS:-1}

function Print {
    local file=/dev/null
    [ '-f' = "$1" ] && file=$2 && shift && shift
    date +"[%F %T] $*" | tee -a $file
}

function Quit {
    while :; do
        pkill -f java && Print killing java ... || break
        sleep 1
    done
    exec 1022<&-
    Print Container stopped.
    test -n "$GOT_SIGTERM"
}

function Usage {
    Print 'This container should run with
    **env TIMEOUT, default 10m(ten minutes)**
    **env MAX_PROCS, default 1**
    **/opt/datax/{log,log_perf,job} mounted from host**
    '
}

function InitPipe {
    Print Initing named pipe ...
    rm -rf pool.pipe
    mkfifo pool.pipe
    exec 1022<> pool.pipe
    rm -rf pool.pipe
    printf "%${MAX_PROCS}s" '' >&1022
}

function StartJob {
    local job="$1"
    local code=0
    Print Starting job $job with timeout $TIMEOUT ...
    timeout ${TIMEOUT} java \
        -server \
        -Xms1g \
        -Xmx1g \
        -Duser.timezone=GMT+08 \
        -XX:+HeapDumpOnOutOfMemoryError \
        -XX:HeapDumpPath=$PWD/log \
        ${JAVA_OPTS:-} \
        -Dfile.encoding=UTF-8 \
        -Dlogback.statusListenerClass=ch.qos.logback.core.status.NopStatusListener \
        -Djava.security.egd=file:///dev/urandom \
        -Ddatax.home=$PWD \
        -Dlogback.configurationFile=$PWD/conf/logback.xml \
        -classpath "$PWD/lib/*:." \
        -Dlog.file.name=$job \
        com.alibaba.datax.core.Engine \
        -mode standalone \
        -jobid -1 \
        -job $PWD/job/$job.json \
        >/dev/null \
        2>log/$job.error \
        || code=$?
    if [ 0 -eq $code ]; then
        Print Job $job finished.
    elif [ 124 -eq $code ]; then
        Print Job $job timeout!
    else
        Print Job $job stopped unexpectly!
    fi
    echo >&1022
}

function StartProc {
    Print Starting datax with max $MAX_PROCS parallel jobs ...
    local job=
    for job in $(ls job/ | grep '\.json$'); do
        read -n 1 -u 1022
        StartJob "${job%.json}" &
    done
    wait
    [ -n "$job" ] && Print All jobs finished. || Print Not found any job!
}

function Main {
    cd /opt/datax
    Usage
    InitPipe
    trap "GOT_SIGTERM=1; Print Got SIGTERM ...; exit" SIGTERM
    StartProc
}

# Start here
Main

