Skip to content

Instantly share code, notes, and snippets.

@WilliamDenniss
Forked from noach-cellogicmobile/heroku.rake
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WilliamDenniss/e8d06e82230fa9dc4853 to your computer and use it in GitHub Desktop.
Save WilliamDenniss/e8d06e82230fa9dc4853 to your computer and use it in GitHub Desktop.
## Restart a Heroku Web Application
## Adapted from a script by mscottford for restarting workers: https://gist.github.com/2028552
## Instructions:
## * Save this script in lib/tasks
## * Gemfile: gem 'heroku-api', :git => 'https://github.com/heroku/heroku.rb.git'
## * Commit Gemfile* and lib/tasks
## * $ heroku config:add APP_NAME='name of the Heroku app'
## * $ heroku config:add HEROKU_API_KEY='the API key found on the Heroku "My Account" page'
## * Deploy and test with $ heroku run rake heroku:webs:restart[10] (Look at process uptime with $ heroku ps)
## ALT: Export the config variables above into the enviroment and run locally $ rake heroku:webs:restart
## * Create a Heroku Scheduler cronjob that runs `rake heroku:webs:restart[10]` to automate restarting at regular intervals
namespace :heroku do
namespace :webs do
desc "Restart the webserver by restarting all Heroku 'web' dynos (optionally sleeping between each process-restart)"
task :restart, [:sleep], [:process_name] do |t, args|
args.with_defaults sleep:0 process_name:"web"
heroku = Heroku::API.new
response = heroku.get_ps(ENV['APP_NAME'])
webs = response.body.map {|item| item['process'] }.select { |item| item =~ /web/ }
webs.each do |web|
puts "Restarting #{web}"
heroku.post_ps_restart(ENV['APP_NAME'], 'ps' => web) rescue nil
sleep args.sleep.to_i
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment