,

Kestrel Queue – Installation in Ubuntu

Introduction

Kestrel is a cool, light-weight persistent message queue server written in Scala that speaks the memcached protocol.
It was originally developed by Robey Pointer (robey) from Twitter.
It was originally a port of its predecessor Starling, which is written in Ruby.

Installation

It’s pretty straight forward to install Kestrel but requires a number steps

# install scala
sudo apt-get install -y scala

# install sbt launcher - create download and build directory
mkdir -p ~/bin/
cd ~/bin/

# download sbt launch
wget http://typesafe.artifactoryonline.com/typesafe/ivy-releases/org.scala-tools.sbt/sbt-launch/0.11.2/sbt-launch.jar
echo 'java -Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -jar `dirname $0`/sbt-launch.jar "$@"' > ./sbt
chmod +x ./sbt

# ensure git is installed
sudo apt-get install -y git

# ensure java jdk is installed
#sudo apt-get install openjdk-7-jdk-y

# create download and build directory
mkdir -p ~/build/kestrel
cd ~/build/kestrel

# download kestrel
git clone git://github.com/robey/kestrel.git

cd kestrel

# build kestrel - automatically downloads required libraries - can take AGES. In my case, it was ~30 mins
~/bin/sbt clean update package-dist

# install kestrel
cd dist/kestrel/
ls

# link the kestrel version to current
ln -s $(ls *SNAPSHOT.jar) kestrel.jar

# create the installation directory
sudo mkdir -p /usr/local/lib/kestrel/
sudo cp -R * /usr/local/lib/kestrel/

# create data directory
sudo mkdir -p /var/spool/kestrel

# create log file directory
sudo mkdir -p /var/log/kestrel

# create pid directory
sudo mkdir -p /var/run/kestrel

Create init.d Script


cat <<- "_EOF_" | sudo tee /etc/init.d/kestrel
#!/bin/bash
APPNAME="kestrel"
ADMIN_PORT=2223
JAR=$(ls /usr/local/lib/kestrel/*SNAPSHOT.jar)
START_CMD="java -jar ${JAR}"
STOP_CMD="wget -qO-  http://localhost:${ADMIN_PORT}/shutdown.txt"
INFOLOG="/var/log/${APPNAME}/output.log"
ERRLOG="/var/log/${APPNAME}/error.log"
PIDFILE="/tmp/${APPNAME}.pid"

mkdir -p /var/log/${APPNAME}/ /tmp/

function getpid(){
   sudo ps aux| grep -e "$START_CMD"|grep -v grep|awk "{print \$2}";
}
function start() {
       PID=`getpid`
                if [ -n "$PID" ]; then
                    echo "$APPNAME is already running with PID $PID."
                    return 0
                fi

                    #echo "$START_CMD > $INFOLOG 2> $ERRLOG & echo \$! > $PIDFILE&"
                    eval "$START_CMD > $INFOLOG 2> $ERRLOG &"
                    PID=`getpid`
                    if [ -n "$PID" ]; then
                       echo "$APPNAME sucessfully started. PID: $PID."
                    else
                       echo "Failed to Start $APPNAME."
                       eval "cat $ERRLOG"
                    fi
}
function stop() {
   PID=`getpid`
   if [ ! -n "$PID" ]; then
       echo "$APPNAME is not running."
       return 0
   fi
   echo "Stopping $APPNAME..."
   if [ -n "$STOP_CMD" ]; then
       eval "$STOP_CMD"
       sleep 1
       PID=`getpid`
       if [ ! -n "$PID" ]; then
          echo "done."
          return 0
       fi
   fi

   kill -3 $PID
   if kill -9 $PID ; then
       sleep 1
   else
       pkill -f "$START_CMD"
   fi
   PID=`getpid`
   if [ ! -n "$PID" ]; then
       echo "done."
   else
       pkill -KILL -f "$START_CMD"
       PID=`getpid`
       if [ ! -n "$PID" ]; then
          echo "done."
       else
          echo "Failed to stop $APPNAME."
       fi
   fi

}
function status(){
  PID=`getpid`
  if [ -n "$PID" ]; then
     echo "$APPNAME is running with PID $PID."
     return 0
  else
     echo "$APPNAME is not running."
     return 1
  fi
}
case "${1:-''}" in
   'start')
           start
        ;;
   'stop')
           stop
        ;;
   'restart')
           stop
           start
          ;;
   'status')
            status
	;;
   *)      # no parameter specified
        echo "Usage: $SELF start|stop|restart|reload|force-reload|status"
        exit 1
        ;;
esac

_EOF_

# set permissions of init.d script
sudo chmod +x /etc/init.d/kestrel


Start and Stop Kestrel Server


# start kestrel
sudo  /etc/init.d/kestrel start
# kestrel sucessfully started. PID: 15881.

# check if server is running :)
sudo  /etc/init.d/kestrel status
#kestrel is running with PID 15881.

References

1. Home: https://github.com/robey/kestrel
2. Service Start and Stop Scripts: http://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html