Created
January 29, 2014 22:34
-
-
Save pavels/8698615 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# init.d script for single or multiple resque installations. Expects at least one .conf | |
# file in /etc/resque | |
# | |
## A sample /etc/resque/my_app.conf | |
## | |
## RAILS_ROOT=/var/apps/www/my_app/current | |
# | |
# If you call this script without any config parameters, it will attempt to run the | |
# init command for all your resque configurations listed in /etc/resque/*.conf | |
# | |
# /etc/init.d/resque start # starts all resques | |
# | |
# If you specify a particular config, it will only operate on that one | |
# | |
# /etc/init.d/resque start my_app | |
# chkconfig: 2345 85 15 | |
# processname: resque | |
# processname: sidekiq | |
# description: Runs resque. | |
# Include RedHat function library | |
. /etc/rc.d/init.d/functions | |
start() { | |
local program | |
local options | |
if [ -f $RAILS_ROOT/Gemfile.lock ]; then | |
program="$BUNDLER exec rake" | |
else | |
program="$RAKE" | |
fi | |
options="RAILS_ENV=$ENVIRONMENT QUEUES=$QUEUES BACKGROUND=yes MUTE=1" | |
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" | |
su -l $USER -c "cd $RAILS_ROOT && $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" | |
su -l $USER -c "cd $RAILS_ROOT && $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 | |
} | |
cmd () { | |
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 | |
} | |
setup () { | |
USER="fantasy" | |
ENVIRONMENT="production" | |
QUEUES="*" | |
COUNT=1 | |
RVM="/usr/local/rvm/bin/rvm 1.9.2-p320 do" | |
BUNDLER="$RVM bundle" | |
RAKE="$RVM rake" | |
PID_DIR="$RAILS_ROOT/tmp/pids" | |
LOG_DIR="$RAILS_ROOT/log" | |
WORKER_PID="$PID_DIR/resque_work_%d.pid" | |
WORKER_LOG="$LOG_DIR/resque_work_%d.log" | |
SCHEDULER_PID="$PID_DIR/scheduler.pid" | |
SCHEDULER_LOG="$LOG_DIR/scheduler.log" | |
} | |
start_stop () { | |
# either run the start/stop/reload/etc command for every config under /etc/resque | |
# or just do it for a specific one | |
# $1 contains the start/stop/etc command | |
# $2 if it exists, should be the specific config we want to act on | |
if [ -f "/etc/resque/$2.conf" ]; then | |
. "/etc/resque/$2.conf" | |
setup | |
echo "$RAILS_ROOT background workers" | |
cmd $1 | |
else | |
files=$(ls /etc/resque/*.conf 2> /dev/null | wc -l) | |
if [ "$files" == "0" ] | |
then | |
echo "[ no workers configured ]" | |
exit 0 | |
fi | |
for CONFIG in /etc/resque/*.conf; do | |
# import the variables | |
. $CONFIG | |
setup | |
if [ ! -d $RAILS_ROOT ] | |
then | |
continue | |
fi | |
echo "$RAILS_ROOT background workers" | |
# run the start/stop/etc command | |
cmd $1 | |
done | |
fi | |
} | |
ARGS="$1 $2" | |
start_stop $ARGS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment