Skip to content

Instantly share code, notes, and snippets.

@tomerd
Created November 30, 2012 23:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tomerd/4179337 to your computer and use it in GitHub Desktop.
Save tomerd/4179337 to your computer and use it in GitHub Desktop.
init.d script to control resque + resque-scheduler with rvm and bundler
#!/bin/sh
#
# chkconfig: - 85 15
# Source function library.
. /etc/rc.d/init.d/functions
ROOT="app_path"
USER="user"
ENVIRONMENT="development"
QUEUES="*"
COUNT=1
RUBY="ruby-1.9.3-p327@global"
GEMS="/usr/local/rvm/gems/$RUBY/bin"
RUBY_ENV="/usr/local/rvm/environments/$RUBY"
BUNDLER="$GEMS/bundle"
RAKE="$GEMS/rake"
PID_DIR="$ROOT/tmp/pids"
LOG_DIR="$ROOT/log"
WORKER_PID="$PID_DIR/resque_worker.%d.pid"
WORKER_LOG="$LOG_DIR/resque_worker.%d.log"
SCHEDULER_PID="$PID_DIR/resque_scheduler.pid"
SCHEDULER_LOG="$LOG_DIR/resque_scheduler.log"
start() {
local program
local options
source $RUBY_ENV
cd $ROOT
if [ -f $ROOT/Gemfile.lock ]; then
program="$BUNDLER exec rake"
else
program="$RAKE"
fi
options="RAILS_ENV=$ENVIRONMENT QUEUES=$QUEUES BACKGROUND=yes"
for i in $(seq 1 $COUNT); do
if [ "$1" = "$i" ] || [ "$#" -eq 0 ]; then
pidfile=$(printf $WORKER_PID $i)
logfile=$(printf $WORKER_LOG $i)
#echo $program $options
echo -n "staring worker $i"
daemon --user $USER "$program resque:work $options PIDFILE=$pidfile 2>&1 >> $logfile"
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo_success
else
echo_failure
fi
echo
fi
done
if [ "$1" = "scheduler" ] || [ "$#" -eq 0 ]; then
echo -n "staring scheduler"
daemon --user $USER "$program resque:scheduler $options PIDFILE=$SCHEDULER_PID 2>&1 >> $SCHEDULER_LOG"
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo_success
else
echo_failure
fi
echo
fi
}
stop() {
local pidfile
for i in $(seq 1 $COUNT); do
if [ "$1" = "$i" ] || [ "$#" -eq 0 ]; then
echo -n "stopping worker $i"
pidfile=$(printf $WORKER_PID $i)
if [ -f $pidfile ]; then
kill -9 $(cat $pidfile)
RETVAL=$?
else
RETVAL=1
fi
if [ $RETVAL -eq 0 ]; then
rm -f $pidfile
echo_success
else
echo_failure
fi
echo
fi
done
if [ "$1" = "scheduler" ] || [ "$#" -eq 0 ]; then
echo -n "stopping scheduler"
if [ -f $SCHEDULER_PID ]; then
kill -9 $(cat $SCHEDULER_PID)
RETVAL=$?
else
RETVAL=1
fi
if [ $RETVAL -eq 0 ]; then
echo_success
rm -f $SCHEDULER_PID
else
echo_failure
fi
echo
fi
}
status() {
local pidfile
for i in $(seq 1 $COUNT); do
if [ "$1" = "$i" ] || [ "$#" -eq 0 ]; then
echo -n "worker $i"
pidfile=$(printf $WORKER_PID $i)
if [ -f $pidfile ]; then
echo
ps -fp $(cat $pidfile)
else
echo " - not running"
fi
fi
done
if [ "$1" = "scheduler" ] || [ "$#" -eq 0 ]; then
echo -n "scheduler"
if [ -f $SCHEDULER_PID ]; then
echo
ps -fp $(cat $SCHEDULER_PID)
else
echo " - not running"
fi
fi
}
case "$1" in
start) start $2 ;;
stop) stop $2 ;;
restart|force-reload)
stop $2
sleep 1
start $2
;;
status) status $2 ;;
*)
echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment