Skip to content

Instantly share code, notes, and snippets.

@jimanx2
Last active September 12, 2017 17:05
Show Gist options
  • Select an option

  • Save jimanx2/95bd3cdf7aefd6cc04c8d27b1c266c89 to your computer and use it in GitHub Desktop.

Select an option

Save jimanx2/95bd3cdf7aefd6cc04c8d27b1c266c89 to your computer and use it in GitHub Desktop.
Simple script to manage puma, resque and resque-scheduler as services
#!/bin/bash
APP=<appname> # directory for this appname must exist!
APPDIR=/var/www/$APP
stop() {
if [ -z "$1" ] || [ "$1" == "web" ]; then
PUMA_PID=`cat $APPDIR/$APP.pid 2>/dev/null`
if [ -n "$PUMA_PID" ]; then
echo "Stopping web (${PUMA_PID})"
kill $PUMA_PID
fi
rm -f $APPDIR/$APP.pid
fi
if [ -z "$1" ] || [ "$1" == "resque" ]; then
RQ_WORKER_PID=`cat $APPDIR/resque.pid 2> /dev/null`
if [ -n "$RQ_WORKER_PID" ]; then
echo "Stopping resque worker (${RQ_WORKER_PID})"
kill $RQ_WORKER_PID
fi
rm -f $APPDIR/resque.pid
fi
if [ -z "$1" ] || [ "$1" == "scheduler" ]; then
RQ_SCHEDULER_PID=`cat $APPDIR/resque-scheduler.pid 2>/dev/null`
if [ -n "$RQ_SCHEDULER_PID" ]; then
echo "Stopping resque-scheduler worker (${RQ_SCHEDULER_PID})"
kill $RQ_SCHEDULER_PID
fi
rm -f $APPDIR/resque-scheduler.pid
fi
}
raise_error()
{
echo "FAILED"
stop
exit
}
start() {
if [ -z "$1" ] || [ "$1" == "web" ]; then
WEB_CONCURRENCY=15 RSSF=yes RAILS_ENV=production /home/deploy/.rbenv/shims/bundle exec puma \
--pidfile $APPDIR/$APP.pid \
--config $APPDIR/current/config/puma.rb \
--redirect-stdout $APPDIR/shared/log/puma.log \
--redirect-stderr $APPDIR/shared/log/puma.log \
--daemon
[[ "$?" == "0" ]] || raise_error
fi
if [ -z "$1" ] || [ "$1" == "resque" ]; then
echo "Starting Resque Worker..."
RAILS_ENV=production /home/deploy/.rbenv/shims/bundle exec rake environment \
resque:work \
LOGFILE=$APPDIR/shared/log/resque.log \
PIDFILE=$APPDIR/resque.pid \
BACKGROUND=yes \
QUEUE=tptest,sms,mailers \
COUNT=5
[[ "$?" == "0" ]] && echo "OK" || raise_error
fi
if [ -z "$1" ] || [ "$1" == "scheduler" ]; then
echo "Starting Resque Scheduler..."
RAILS_ENV=production /home/deploy/.rbenv/shims/bundle exec rake environment \
resque:scheduler \
LOGFILE=$APPDIR/shared/log/resque-scheduler.log \
PIDFILE=$APPDIR/resque-scheduler.pid \
BACKGROUND=yes
[[ "$?" == "0" ]] && echo "OK" || raise_error
fi
}
case $1 in
stop)
stop $2
;;
start)
start $2
;;
restart)
stop $2
start $2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment