jubatus-recommenderの起動スクリプト


#前提条件
- AmazonLinuxで起動できたスクリプトです
- configurationのJSONファイルのパスは適宜変更ください。
- zookeeper + jubaconfig + jubarecommender_proxy + jubavisor + jubavisor の構成をjubactlで起動する形です。

#設置場所
/etc/init.d に配置。

#使い方

```shell-session
$ sudo service jubatus-recommender
Usage : /etc/init.d/jubatus-recommender (start|stop|status|restart)

# START
$ sudo service jubatus-recommender start
Starting jubatus-recommender:
JMX enabled by default
Using config: /home/ec2-user/zookeeper-X.X.X/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
3669 INFO  [zk.cpp:586] got ZooKeeper event: type SESSION_EVENT(-1), state CONNECTED_STATE(3)
3669 INFO  [zk.cpp:597] ZooKeeper session established, negotiated timeout 10000 ms
3667 INFO  [zk.cpp:113] connected to zk: 127.0.0.1:2181
3672 INFO  [zk.cpp:586] got ZooKeeper event: type SESSION_EVENT(-1), state CONNECTED_STATE(3)
3672 INFO  [zk.cpp:597] ZooKeeper session established, negotiated timeout 10000 ms
3670 INFO  [zk.cpp:113] connected to zk: 127.0.0.1:2181
3670 INFO  [config.cpp:143] set config to zookeeper: /jubatus/config/recommender/XXXX
Recommender Proxy Started                                  [  OK  ]
Jubavisor Started                                          [  OK  ]
3689 INFO  [zk.cpp:586] got ZooKeeper event: type SESSION_EVENT(-1), state CONNECTED_STATE(3)
3689 INFO  [zk.cpp:597] ZooKeeper session established, negotiated timeout 10000 ms
3687 INFO  [zk.cpp:113] connected to zk: 127.0.0.1:2181
sending start / jubarecommender/XXXX to XXX.XXX.XXX.XXX_9200...ok.
Recommender Started                                        [  OK  ]
3708 INFO  [zk.cpp:586] got ZooKeeper event: type SESSION_EVENT(-1), state CONNECTED_STATE(3)
3708 INFO  [zk.cpp:597] ZooKeeper session established, negotiated timeout 10000 ms
3706 INFO  [zk.cpp:113] connected to zk: 127.0.0.1:2181
sending load / XXXX to XXX.XXX.XXX.XXX_9201...ok.

# STATUS
$ sudo service jubatus-recommender status
jubatus-recommender (pid  XXXX) is running...
3726 INFO  [zk.cpp:586] got ZooKeeper event: type SESSION_EVENT(-1), state CONNECTED_STATE(3)
3726 INFO  [zk.cpp:597] ZooKeeper session established, negotiated timeout 10000 ms
3724 INFO  [zk.cpp:113] connected to zk: 127.0.0.1:2181
active jubaproxy members:
XXX.XXX.XXX.XXX_9199
active jubavisor members:
XXX.XXX.XXX.XXX_9200
active XXXX members:
XXX.XXX.XXX.XXX_9201

# STOP
$ sudo service jubatus-recommender stop
Stopping jubatus-recommender:
2825 INFO  [zk.cpp:586] got ZooKeeper event: type SESSION_EVENT(-1), state CONNECTED_STATE(3)
2825 INFO  [zk.cpp:597] ZooKeeper session established, negotiated timeout 10000 ms
2823 INFO  [zk.cpp:113] connected to zk: 127.0.0.1:2181
sending save / XXXX to XXX.XXX.XXX.XXX_9201...ok.
2828 INFO  [zk.cpp:586] got ZooKeeper event: type SESSION_EVENT(-1), state CONNECTED_STATE(3)
2828 INFO  [zk.cpp:597] ZooKeeper session established, negotiated timeout 10000 ms
2826 INFO  [zk.cpp:113] connected to zk: 127.0.0.1:2181
sending stop / jubarecommender/XXXX to XXX.XXX.XXX.XXX_9200...ok.
Recommender Stopped                                        [  OK  ]
Recommender Proxy Stopped                                  [  OK  ]
Jubavisor Stopped                                          [  OK  ]
JMX enabled by default
Using config: /home/ec2-user/zookeeper-X.X.X/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED

```

#起動スクリプト
- この方のスクリプトを参考にしました。大変参考になりました。ありがとうございました。
- [jubatus - jubaclassifier 起動スクリプト （スタンドアロン） - Qiita](http://qiita.com/oggy8021/items/fc31633b22fe97caaf56)

```jubatus-recommender.sh
#!/bin/sh
#
# chkconfig: - 99 32

# Source function library
. /etc/init.d/functions

SERVICENAME="jubatus-recommender"
RETVAL=0

PROXY_PIDFILE=/var/run/jubatus-recommender_proxy.pid
RECOMMENDER_PIDFILE=/var/run/jubatus-recommender.pid
VISOR_PIDFILE=/var/run/jubatus-visor.pid
PREFIX_HOST=`/sbin/ifconfig | grep 'inet addr' | grep -v 127.0.0.1 | awk '{print $2;}' | cut -d: -f2`
JUBACONFIG=/usr/bin/jubaconfig
JUBARECOMMENDER_PROXY=/usr/bin/jubarecommender_proxy
JUBARECOMMENDER=/usr/bin/jubarecommender
JUBAVISOR=/usr/bin/jubavisor
JUBACTL=/usr/bin/jubactl
JUBATUS_TYPE=recommender
RECOMMENDER_NAME=XXXX  # 適宜設定してください
RECOMMENDER_PORT=9200
ZOOKEEPER=/home/ec2-user/zookeeper-X.X.X/bin/zkServer.sh  # 適宜設定してください
ZOOKEEPER_HOST=localhost
ZOOKEEPER_PORT=2181

CONFIG=/etc/jubatus/config.json
LOGDIR=/var/log/jubatus
MODELDIR=/tmp
LOGCONFIG=/usr/share/jubatus/example/log/log4cxx.xml
MODEL_FILE=/tmp/${PREFIX_HOST}_9201_${JUBATUS_TYPE}_${RECOMMENDER_NAME}.jubatus

start() {
    ## ロックファイルの確認
    if [ -f $PROXY_PIDFILE -a -f $VISOR_PIDFILE -a -f $RECOMMENDER_PIDFILE ]; then
        echo "Jubatus Recommender Service is already started"
        exit 1
    fi

    echo "Starting ${SERVICENAME}:"
    ## ログフォルダがない場合に作成する
    if [ ! -e ${LOGDIR} ]; then
        mkdir ${LOGDIR}
    fi
    ${ZOOKEEPER} start
    wait
    ${JUBACONFIG} --cmd delete --zookeeper=${ZOOKEEPER_HOST}:${ZOOKEEPER_PORT} --file ${CONFIG} --name ${RECOMMENDER_NAME} --type ${JUBATUS_TYPE}
    wait
    ${JUBACONFIG} --cmd write --zookeeper=${ZOOKEEPER_HOST}:${ZOOKEEPER_PORT} --file ${CONFIG} --name ${RECOMMENDER_NAME} --type ${JUBATUS_TYPE}
    wait
    sleep 2
    ${JUBARECOMMENDER_PROXY} --zookeeper ${ZOOKEEPER_HOST}:${ZOOKEEPER_PORT} -g ${LOGCONFIG} -l ${LOGDIR} &
    RETVAL=$?
    if [ ${RETVAL} = 0 ]; then
        success
        touch /var/lock/subsys/${SERVICENAME}_proxy
        /sbin/pidof ${JUBARECOMMENDER_PROXY} > ${PROXY_PIDFILE}
        echo "Recommender Proxy Started"
    else
        failure
    fi

    ${JUBAVISOR} --rpc-port=${RECOMMENDER_PORT} --zookeeper ${ZOOKEEPER_HOST}:${ZOOKEEPER_PORT} -g ${LOGCONFIG} -l ${LOGDIR} --daemon
    RETVAL=$?
    if [ ${RETVAL} = 0 ]; then
        success
        touch /var/lock/subsys/${SERVICENAME}_visor
        /sbin/pidof ${JUBAVISOR} > ${VISOR_PIDFILE}
       echo "Jubavisor Started"
    else
        failure
    fi

    ${JUBACTL} -c start --server=jubarecommender --type=recommender --name=${RECOMMENDER_NAME} --zookeeper ${ZOOKEEPER_HOST}:${ZOOKEEPER_PORT} -G ${LOGCONFIG} -L ${LOGDIR}
    RETVAL=$?
    if [ ${RETVAL} = 0 ]; then
        success
        touch /var/lock/subsys/${SERVICENAME}
        /sbin/pidof ${JUBARECOMMENDER} > ${RECOMMENDER_PIDFILE}
        echo "Recommender Started"
    else
        failure
    fi

    sleep 3
    ${JUBACTL} -c load --server=jubarecommender --type=recommender --name=${RECOMMENDER_NAME} --zookeeper ${ZOOKEEPER_HOST}:${ZOOKEEPER_PORT}
    RETVAL=$?
    if [ ${RETVAL} = 0 ]; then
        success
    else
        failure
    fi
    echo
}

stop() {
    ## ロックファイルの確認
    if [ ! -e $PROXY_PIDFILE -a ! -e $VISOR_PIDFILE -a ! -e $RECOMMENDER_PIDFILE ]; then
        echo "Jubatus Recommender Service is already stopped"
        exit 1
    fi

    echo "Stopping ${SERVICENAME}:"
    if [ -f $RECOMMENDER_PIDFILE ]; then
        ${JUBACTL} -c save --server=jubarecommender --type=recommender --name=${RECOMMENDER_NAME} --zookeeper ${ZOOKEEPER_HOST}:${ZOOKEEPER_PORT}
        ${JUBACTL} -c stop --server=jubarecommender --type=recommender --name=${RECOMMENDER_NAME} --zookeeper ${ZOOKEEPER_HOST}:${ZOOKEEPER_PORT}
        RETVAL=$?
        if [ ${RETVAL} -eq 0 ]; then
            rm -f /var/lock/subsys/${SERVICENAME}
            rm -f ${RECOMMENDER_PIDFILE}
            success
            echo "Recommender Stopped"
        fi
    fi

    if [ -f $PROXY_PIDFILE ]; then
        kill -KILL `/sbin/pidof $JUBARECOMMENDER_PROXY`
        RETVAL=$?
        if [ ${RETVAL} -eq 0 ]; then
            rm -f /var/lock/subsys/${SERVICENAME}_proxy
            rm -f ${PROXY_PIDFILE}
            success
            echo "Recommender Proxy Stopped"
        fi
    fi

    if [ -f $VISOR_PIDFILE ]; then
        kill -KILL `/sbin/pidof $JUBAVISOR`
        RETVAL=$?
        if [ ${RETVAL} -eq 0 ]; then
            rm -f /var/lock/subsys/${SERVICENAME}_visor
            rm -f ${VISOR_PIDFILE}
            success
            echo "Jubavisor Stopped"
        fi
    fi

    ${ZOOKEEPER} stop
    echo
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status ${SERVICENAME}
        ${JUBACTL} -c status --server=jubarecommender --type=recommender --name=${RECOMMENDER_NAME} --zookeeper ${ZOOKEEPER_HOST}:${ZOOKEEPER_PORT}
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage : $0 (start|stop|status|restart)"
        RETVAL=1
esac

exit ${RETVAL}
```