Архив рубрики: sidekiq

Sidekiq service

https://cdyer.co.uk/blog/init-script-for-sidekiq-with-rbenv/

Sidekiq is a great queuing system for any rails app, it is easy to setup and fast since it also uses Redis. The most difficult part of integrating with a recent app for me, was getting it to start when the VPS booted.

The people behind sidekiq thankfully provide a nice init script, although my particular setup caused a few problems for it.

  • The script should run as root, however I use a ‘deployer’ user, under which runs rbenv. Root had no access to bundle exec
  • Since I don’t write init scripts often, it took me a while to get it to startafter Redis

My workaround to the rbenv problem was to modify the providied Sidekiq init script, to run the actual bundle exec sidekiq command as my deployment user:

START_CMD="$BUNDLE exec $SIDEKIQ -e $APP_ENV -P $PID_FILE"  
CMD="cd ${APP_DIR}; ${START_CMD}"

# Replaced this:
$START_CMD >> $LOG_FILE 2>&1 &

# With this:
su -c "$CMD" - $AS_USER  

I created the file /etc/init.d/sidekiq with the contents of the sidekiq init script, and my code above. This didn’t work. Examining the log file, sidekiq was being started before redis could finish initializing. To stop this happening I needed to run:

sudo update-rc.d sidekiq defaults 99  

This sets the index of the sidekiq script to be 99. For me Redis was initing at 20, since 99 is greater than 20, Sidekiq will start after.

You can now manage the sidekiq process with the usual:

service sidekiq start  
service sidekiq status  
service sidekiq stop  

Full modified init script below. I have only tried it on Ubuntu 12.10 although it should also work on other Ubuntus and probably Debian.

#!/bin/bash
# sidekiq Init script for Sidekiq
# chkconfig: 345 100 75
#
# Description: Starts and Stops Sidekiq message processor for Stratus application.
#
# User-specified exit parameters used in this script:
#
# Exit Code 5 — Incorrect User ID
# Exit Code 6 — Directory not found
# You will need to modify these
APP=«myapp«
AS_USER=«myuser«
APP_DIR=«/var/www/${APP}/current«
APP_CONFIG=«${APP_DIR}/config«
LOG_FILE=«$APP_DIR/log/sidekiq.log«
LOCK_FILE=«$APP_DIR/${APP}-lock«
PID_FILE=«$APP_DIR/${APP}.pid«
GEMFILE=«$APP_DIR/Gemfile«
SIDEKIQ=«sidekiq«
APP_ENV=«production«
BUNDLE=«bundle«
START_CMD=«$BUNDLE exec $SIDEKIQ -e $APP_ENV -P $PID_FILE«
CMD=«cd ${APP_DIR}; ${START_CMD} >> ${LOG_FILE} 2>&1 &«
RETVAL=0
start() {
status
if [ $? -eq 1 ]; then
[ `id -u` == '0' ] || (echo «$SIDEKIQ runs as root only ..«; exit 5)
[ -d $APP_DIR ] || (echo «$APP_DIR not found!.. Exiting«; exit 6)
cd $APP_DIR
echo «Starting $SIDEKIQ message processor .. «
su -c «$CMD«$AS_USER
RETVAL=$?
#Sleeping for 8 seconds for process to be precisely visible in process table — See status ()
sleep 8
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
return $RETVAL
else
echo «$SIDEKIQ message processor is already running .. «
fi
}
stop() {
echo «Stopping $SIDEKIQ message processor ..«
SIG=«INT«
kill -$SIG `cat $PID_FILE`
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
return $RETVAL
}
status() {
ps -ef | grep sidekiq [0-9].[0-9].[0-9] | grep -v grep
return $?
}
case «$1« in
start)
start
;;
stop)
stop
;;
status)
status
if [ $? -eq 0 ]; then
echo «$SIDEKIQ message processor is running ..«
RETVAL=0
else
echo «$SIDEKIQ message processor is stopped ..«
RETVAL=1
fi
;;
*)
echo «Usage: $0 {start|stop|status}«
exit 0
;;
esac
exit $RETVAL