Skip to content

Commit

Permalink
use upstart for resque web and worker pool
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Conway committed Jan 27, 2012
1 parent c2b5c23 commit 5836d63
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 84 deletions.
@@ -1,14 +1,10 @@
<%
@path = '/etc/monit/monit.d/monit-resque_web.conf'

pidfile = "#{Rubber.root}/tmp/pids/resque_web.pid"
start_program = "HOME=/root resque-web --pid-file #{pidfile} --port #{rubber_env.resque_web_port} --no-launch #{Rubber.root}/config/initializers/resque.rb"
stop_program = "HOME=/root resque-web --pid-file #{pidfile} --kill"
%>

check process resque_web with pidfile <%= pidfile %>
group resque_web
start program = "/bin/bash -l -c '<%= start_program %>'"
stop program = "/bin/bash -l -c '<%= stop_program %>'"
check process resque_web with pidfile <%= rubber_env.resque_web_pid_file %>
group resque-<%= Rubber.env %>
start program = "/usr/bin/env service resque-web start"
stop program = "/usr/bin/env service resque-web stop"

if totalmem > 350.0 MB for 15 cycles then restart
@@ -0,0 +1,8 @@
<%
@path = '/etc/monit/monit.d/monit-resque_pool.conf'
%>

check process resque_pool with pidfile <%= rubber_env.resque_pool_pid_file %>
group resque-<%= Rubber.env %>
start program = "/usr/bin/env service resque-pool start"
stop program = "/usr/bin/env service resque-pool stop"

This file was deleted.

41 changes: 33 additions & 8 deletions templates/resque/config/rubber/deploy-resque.rb
Expand Up @@ -15,22 +15,23 @@

desc "Starts resque workers"
task :start, :roles => :resque_worker do
rsudo "#{current_path}/script/resque-pool-ctl start", :as => rubber_env.app_user
rsudo "service resque-pool start"
end

desc "Stops resque workers and monitor"
task :stop, :roles => :resque_worker do
rsudo "#{current_path}/script/resque-pool-ctl stop"
rsudo "service resque-pool stop || true"
end

desc "Force kill all resque workers and monitor"
task :force_stop, :roles => :resque_worker do
rsudo "#{current_path}/script/resque-pool-ctl force-stop"
rsudo "kill -TERM `cat #{rubber_env.resque_pool_pid_file}`"
end

desc "Restarts resque workers"
task :restart, :roles => :resque_worker do
rsudo "#{current_path}/script/resque-pool-ctl restart", :as => rubber_env.app_user
stop
start
end

desc "Continuously show worker stats"
Expand Down Expand Up @@ -162,25 +163,49 @@ def uid

namespace :web do
rubber.allow_optional_tasks(self)

after "rubber:bootstrap", "rubber:resque:web:bootstrap"

task :bootstrap, :roles => :resque_web do
exists = capture("echo $(ls #{rubber_env.redis_db_dir} 2> /dev/null)")
if exists.strip.size == 0

rubber.sudo_script 'bootstrap_redis', <<-ENDSCRIPT
mkdir -p #{rubber_env.redis_db_dir}
chown -R redis:redis #{rubber_env.redis_db_dir}
ENDSCRIPT

# After everything installed on machines, we need the source tree
# on hosts in order to run rubber:config for bootstrapping the db
rubber.update_code_for_bootstrap

# Gen just the conf for cassandra
rubber.run_config(:file => "role/redis", :force => true, :deploy_path => release_path)

end

restart
end

before "deploy:stop", "rubber:resque:web:stop"
after "deploy:start", "rubber:resque:web:start"
after "deploy:restart", "rubber:resque:web:restart"

desc "Starts resque web tools"
task :start, :roles => :resque_web do
rsudo "RAILS_ENV=#{Rubber.env} resque-web --pid-file #{Rubber.root}/tmp/pids/resque_web.pid --port #{rubber_env.resque_web_port} --no-launch #{current_path}/config/initializers/resque.rb", :as => rubber_env.app_user
rsudo "service resque-web start"
end

desc "Stops resque web tools"
task :stop, :roles => :resque_web do
rsudo "RAILS_ENV=#{Rubber.env} resque-web --pid-file #{Rubber.root}/tmp/pids/resque_web.pid --kill", :as => rubber_env.app_user
rsudo "service resque-web stop || true"
end

desc "Restarts resque web tools"
task :restart, :roles => :resque_web do
rubber.resque.web.stop
rubber.resque.web.start
stop
sleep 2
start
end

end
Expand Down
@@ -0,0 +1,20 @@
<%
@path = "/etc/init/resque-web.conf"
@backup = false
%>
description "resque web server"

start on started redis
stop on runlevel [016]

script
exec sudo -u <%= rubber_env.app_user %> /bin/bash -l -c 'cd <%= Rubber.root %> && exec bundle exec resque-web --foreground --port <%= rubber_env.resque_web_port %> --no-launch --app-dir tmp/resque_web config/initializers/resque.rb > log/resque-web.log 2>&1 < /dev/null'
end script

post-start script
status resque-web | head -n1 | awk '{print $NF}' > <%= rubber_env.resque_web_pid_file %>
end script

post-stop script
rm -f <%= rubber_env.resque_web_pid_file %>
end script
87 changes: 32 additions & 55 deletions templates/resque/script/resque-pool-ctl → ...le/resque_worker/resque-pool-upstart.conf 100755 → 100644
@@ -1,8 +1,35 @@
#!/bin/bash

root=`dirname $0`/..
pidfile=$root/tmp/pids/resque-pool.pid

<%
@path = "/etc/init/resque-pool.conf"
@backup = false
%>
description "resque pool server"

start on started redis
stop on runlevel [016]

# not available in upstart 0.6.5
# kill signal INT

script
cd <%= Rubber.root %>
exec sudo -u <%= rubber_env.app_user %> /bin/bash -l -c 'cd <%= Rubber.root %> && exec bundle exec resque-pool > log/resque-pool.log 2>&1 < /dev/null'
end script

post-start script
status resque-pool | head -n1 | awk '{print $NF}' > <%= rubber_env.resque_pool_pid_file %>
end script

# using pre-stop prevents restarts in upstart on ubuntu 10.04,
# but no choice since "kill signal" not available in that ver
# of upstart, and we need to kill INT on restart during deploy
# so that we dont end up killing running jobs
#
pre-stop script
pid=`cat /var/run/resque-pool.pid`
kill -INT $pid
while ps -p $pid > /dev/null; do sleep 0.1; done
rm -f <%= rubber_env.resque_pool_pid_file %>
end script

# The pool manager responds to the following signals:
#
Expand All @@ -21,53 +48,3 @@ pidfile=$root/tmp/pids/resque-pool.pid
# USR1 - Immediately kill child but don't exit
# USR2 - Don't start to process any new jobs
# CONT - Start to process new jobs again after a USR2

function start
{
bundle exec resque-pool -d
}

function stop
{
signal=$1

if [[ -f $pidfile ]]; then
cat $pidfile | xargs kill -$signal
exit 0
fi
}

function restart
{
if [[ -f $pidfile ]]; then
cat $pidfile | xargs kill -HUP;
if [[ $? != 0 ]]; then
start
fi
else
start
fi
}

cd $root
mkdir -p `dirname $pidfile`

case "$1" in
start)
start
;;
stop)
stop INT
;;
force-stop)
stop TERM
;;
restart)
restart
;;
*)
echo "Usage: `basename $0` {start|stop|force-stop|restart}"
exit 1
;;
esac

2 changes: 2 additions & 0 deletions templates/resque/config/rubber/rubber-resque.yml
@@ -1,4 +1,6 @@
resque_web_port: 5678
resque_web_pid_file: /var/run/resque-web.pid
resque_pool_pid_file: /var/run/resque-pool.pid

# list of workers with the queues to be run on each - on each worker instance,
# a worker is run for each item in this list to handle the queues specified
Expand Down

0 comments on commit 5836d63

Please sign in to comment.