#!@l_prefix@/bin/openpkg rc
##
##  rc.tidb -- Run-Commands
##

%config
    tidb_enable="$openpkg_rc_def"
    tidb_flags="-L info -P 4000 -path @l_prefix@/var/tidb/db -store goleveldb"
    tidb_log_prolog="true"
    tidb_log_epilog="true"
    tidb_log_numfiles="10"
    tidb_log_minsize="1M"
    tidb_log_complevel="9"

%common
    tidb_pidfile="@l_prefix@/var/tidb/run/tidb.pid"
    tidb_logfile="@l_prefix@/var/tidb/log/tidb.log"
    tidb_signal () {
        [ -f $tidb_pidfile ] && kill -$1 `cat $tidb_pidfile`
    }

%status -u @l_rusr@ -o
    tidb_usable="unknown"
    tidb_active="no"
    rcService tidb enable yes && \
        tidb_signal 0 && tidb_active="yes"
    echo "tidb_enable=\"$tidb_enable\""
    echo "tidb_usable=\"$tidb_usable\""
    echo "tidb_active=\"$tidb_active\""

%start -u @l_rusr@
    rcService tidb enable yes || exit 0
    rcService tidb active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/tidb-server $tidb_flags \
        </dev/null >>$tidb_logfile 2>&1 &
        echo $! >$tidb_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService tidb enable yes || exit 0
    rcService tidb active no  && exit 0
    tidb_signal TERM
    sleep 2
    rm -f $tidb_pidfile >/dev/null 2>&1 || true

%restart -u @l_rusr@
    rcService tidb enable yes || exit 0
    rcService tidb active no  && exit 0
    rc tidb stop start

%daily -u @l_rusr@
    rcService tidb enable yes || exit 0
    shtool rotate -f \
        -n ${tidb_log_numfiles} -s ${tidb_log_minsize} -d \
        -z ${tidb_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${tidb_log_prolog}" \
        -E "${tidb_log_epilog}; rc tidb restart" \
        $tidb_logfile

