EGI-FCTF/oneacct_export

View on GitHub
examples/etc/init.d/oneacct-export-sidekiq

Summary

Maintainability
Test Coverage
#!/bin/sh

### BEGIN INIT INFO
# Provides:          oneacct-export-sidekiq
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop oneacct-export sidekiq server
# Description:       Start the sidekiq server required by oneacct-export.
### END INIT INFO

run_dir="/var/run/oneacct-export"
log_dir="/var/log/oneacct-export"
conf_dir="/etc/oneacct-export"

omnibus_base_dir="/opt/oneacct-export"
embedded_base_dir="$omnibus_base_dir/embedded"
embedded_ruby_version=`$embedded_base_dir/bin/ruby -e "puts RUBY_VERSION.split('.').fill('0',2..2).join('.')"`
embedded_oneacct_version=`$omnibus_base_dir/bin/oneacct-export --version`
embedded_oneacct_dir="$embedded_base_dir/lib/ruby/gems/$embedded_ruby_version/gems/oneacct-export-$embedded_oneacct_version"

user="apel"
cmd="$omnibus_base_dir/bin/sidekiq -r $embedded_oneacct_dir/lib/one_worker.rb -C $conf_dir/sidekiq.yml"

name=`basename $0`
pid_file="$run_dir/sidekiq.init.pid"
stdout_log="$log_dir/sidekiq.init.stdout.log"
stderr_log="$log_dir/sidekiq.init.stderr.log"

retval=0

if [ "x$SSL_CERT_DIR" = "x" ]; then
    SSL_CERT_DIR="/etc/grid-security/certificates"
fi

if [ "x$DEBUG" = "x1" ]; then
    ONEACCT_EXPORT_LOG_LEVEL="DEBUG"
else
    ONEACCT_EXPORT_LOG_LEVEL="ERROR"
fi

get_pid() {
    cat "$pid_file"
}

setup_dirs() {
    mkdir -p "$run_dir"
    chown "$user":"$user" "$run_dir"
}


is_running() {
    [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}

start() {
    if is_running; then
        echo "Already started"
    else
        echo "Starting $name ..."
        setup_dirs

        cd "$run_dir"
        sudo -u "$user" RAILS_ENV=production SSL_CERT_DIR=$SSL_CERT_DIR ONEACCT_EXPORT_LOG_LEVEL=$ONEACCT_EXPORT_LOG_LEVEL $cmd >> "$stdout_log" 2>> "$stderr_log" &

        echo $! > "$pid_file"
        if ! is_running; then
            echo "Unable to start, see $stdout_log and $stderr_log"
            retval=1
        else
            echo "Started"
        fi
    fi
}

stop() {
    if is_running; then
        echo "Stopping $name ..."

        kill `get_pid`
        for i in $(seq 1 15); do
            if ! is_running; then
                break
            fi
            echo -n "."
            sleep 1
        done

        echo
        if is_running; then
            echo "Not stopped; may still be shutting down or shutdown may have failed"
            retval=1
        else
            echo "Stopped"
            if [ -f "$pid_file" ]; then
                rm "$pid_file"
            fi
        fi
    else
        echo "Not running"
        retval=1
    fi
}

restart(){
    stop

    if is_running; then
        echo "Unable to stop, will not attempt to start"
        retval=1
    else
        start
    fi
}

case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    restart
    ;;
    status)
    if is_running; then
        echo "Running"
    else
        echo "Stopped"
        retval=3
    fi
    ;;
    *)
    echo "Usage: $name {start|stop|restart|status}"
    retval=1
    ;;
esac

exit $retval