Skip to content

Instantly share code, notes, and snippets.

@Schniz
Forked from markoa/deploy.rb
Last active April 8, 2018 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Schniz/6685336 to your computer and use it in GitHub Desktop.
Save Schniz/6685336 to your computer and use it in GitHub Desktop.
Files for using capistrano and resque. some shell magic for killin those resque workers. pay attention to the `w.stop` stuff - it should kill all the resque proccesses (!!) when the god stops it. yeah.
namespace :god do
desc "Hot-reload God configuration for the new release Resque worker"
task :reload_release do
run "cd #{release_path} && god && god load config/resque.god && god restart resque"
end
desc "Hot-reload God configuration for the current release Resque worker"
task :reload_current do
run "cd #{current_path} && god && god load config/resque.god && god restart resque"
end
desc "Terminates god :("
task :terminate do
run "god terminate"
end
end
namespace :resque do
desc "Kills all the resque workers"
task :kill_all do
run "ps aux | grep resque | grep -v grep | awk '{print $2}' | xargs --no-run-if-empty kill"
end
end
RESQUE_WORKER_COUNT = ENV['RESQUE_WORKER_COUNT'] || 1
RESQUE_RAILS_ENV = ENV['RAILS_ENV'] || 'production'
RESQUE_GEMFILE = ENV['RESQUE_GEMFILE'] || '/var/www/myapp/current/Gemfile'
God.watch do |w|
w.name = 'resque'
w.interval = 30.seconds
w.env = { 'RAILS_ENV' => RESQUE_RAILS_ENV, 'COUNT' => RESQUE_WORKER_COUNT.to_s, 'QUEUE' => '*', 'GEMFILE' => RESQUE_GEMFILE }
w.dir = File.expand_path(File.join(File.dirname(__FILE__),'..'))
w.start = "bundle exec rake resque:workers"
w.start_grace = 10.seconds
w.stop = "ps aux | grep resque | grep -v grep | awk '{print $2}' | xargs --no-run-if-empty kill"
w.log = File.expand_path(File.join(File.dirname(__FILE__), '../../../shared/','log','resque-worker.log'))
# restart if memory gets too high
w.transition(:up, :restart) do |on|
on.condition(:memory_usage) do |c|
c.above = 100.megabytes
c.times = 2
end
end
# determine the state on startup
w.transition(:init, { true => :up, false => :start }) do |on|
on.condition(:process_running) do |c|
c.running = true
end
end
# determine when process has finished starting
w.transition([:start, :restart], :up) do |on|
on.condition(:process_running) do |c|
c.running = true
c.interval = 5.seconds
end
# failsafe
on.condition(:tries) do |c|
c.times = 5
c.transition = :start
c.interval = 5.seconds
end
end
# start if process is not running
w.transition(:up, :start) do |on|
on.condition(:process_running) do |c|
c.running = false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment