Why do we need to stop resque worker when we deploy the code to the server?
This is part of my deploy file. I found there is a possible error in the codes:
namespace :resque do
desc "Start resque workers"
task :start do
# Start two workers with separate run commands, so we can store their PIDs
# Hacky, but works
run "if [ ! -e #{deploy_to}/shared/pids/resque_production_1.pid ]; then cd #{deploy_to}/current && RAILS_ENV=production QUEUE=* PIDFILE=#{deploy_to}/shared/pids/resque_production_1.pid BACKGROUND=yes VERBOSE=1 bundle exec rake environment resque:work; fi;"
run "if [ ! -e #{deploy_to}/shared/pids/resque_production_2.pid ]; then cd #{deploy_to}/current && RAILS_ENV=production QUEUE=* PIDFILE=#{deploy_to}/shared/pids/resque_production_2.pid BACKGROUND=yes VERBOSE=1 bundle exec rake environment resque:work; fi;"
end
desc "Stop resque workers"
task :stop do
run "if [ -e #{deploy_to}/shared/pids/resque_production_2.pid ]; then echo \"Killing Worker #1\"; kill -s QUIT `cat #{deploy_to}/shared/pids/resque_production_2.pid`; rm -f #{deploy_to}/shared/pids/resque_production_2.pid; echo \"Done\"; fi;"
run "if [ -e #{deploy_to}/shared/pids/resque_production_2.pid ]; then echo \"Killing Worker #2\"; kill -s QUIT `cat #{deploy_to}/shared/pids/resque_production_2.pid`; rm -f #{deploy_to}/shared/pids/resque_production_2.pid; echo \"Done\"; fi;"
end
It seems there are some errors :
# stop resque worker
/resque_production_2.pid
both of them kill /resque_production_2.pid ...This means one of the worker is not killed during the deploy...Do you think this would cause any problem...
Because I recently found that one of my resque job cannot be queued into the queue in production server . And without showing a fail in the list. Might it cause by this?.But it works fine in staging server. Also other resque jobs in production server can work fine. This is very weird.
Resque workers are restarted during deploy to make sure that the latest version of your code (generally models and workers) is running in the worker. Failing to restart the worker means that the worker is running the revision of your code that it was last started from; if you have made significantly functionality changes, then you could be running code that will break or otherwise introduce bugs since your current code may not be compatible with the version of the code that it's running.
As an aside, something like the following form will help prevent bugs like this in the future (as well as letting you add additional workers just by incrementing the upper bound):
WORKER_COUNT = 2
task :start do
# Start n workers with separate run commands, so we can store their PIDs
1.upto(WORKER_COUNT) do |i|
run "if [ ! -e #{deploy_to}/shared/pids/resque_production_#{i}.pid ]; then cd #{deploy_to}/current && RAILS_ENV=production QUEUE=* PIDFILE=#{deploy_to}/shared/pids/resque_production_#{i}.pid BACKGROUND=yes VERBOSE=1 bundle exec rake environment resque:work; fi;"
end
end
desc "Stop resque workers"
task :stop do
1.upto(WORKER_COUNT) do |i|
run "if [ -e #{deploy_to}/shared/pids/resque_production_#{i}.pid ]; then echo \"Killing Worker #1\"; kill -s QUIT `cat #{deploy_to}/shared/pids/resque_production_#{i}.pid`; rm -f #{deploy_to}/shared/pids/resque_production_#{i}.pid; echo \"Done\"; fi;"
end
end