DanielBlanco
11/24/2014 - 7:21 PM

Restart resque workers after server reboot

Restart resque workers after server reboot

#!/bin/bash
# This is an sample file, put a copy of this file inside /shared/bin
# I'm assuming a capistrano directory structure under /var/www/my-project/
#
# Called by crontab when the server reboots
#
# To use this file execute with root privileges:
#   crontab -e
#
# then add this line:
#   @reboot /<full-path-to-project>/shared/bin/server-reboot
#
# Make sure that this is an executable file (chmod 755 my_command).
#
PROJECT_PATH="/var/www/my-project"
PIDS_PATH="$PROJECT_PATH/shared/tmp/pids"
ENV="RAILS_ENV=production"
RVM="/home/webappuser/.rvm/bin/rvm"

echo "Restarting 2 resque workers..."

function restart_worker {
  local PID_FILE="$PIDS_PATH/resque_work_$1.pid"

  # This will kill any existing pid
  if [ -f "$PID_FILE" ]
  then
    pid=$(cat $PID_FILE)
    kill -0 $pid > /dev/null 2>&1
    echo "Worker $pid stopped"
    rm $PID_FILE
    echo "$PID_FILE deleted"
  fi

  # This will start the pid
  cd "$PROJECT_PATH/current" && ( $RVM default do bundle exec rake $ENV QUEUE="my_queue" PIDFILE=$PID_FILE BACKGROUND=yes VERBOSE=1 INTERVAL=5 environment resque:work >> /dev/null 2>> /dev/null )
  echo "$PID_FILE worker file created"
}

# Right now we have 2 workers.
for i in `seq 1 2`;
do
  restart_worker $i
done