-
-
Save noach-cellogicmobile/3116069 to your computer and use it in GitHub Desktop.
Rake task for restarting a Heroku Web Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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] do |t, args| | |
args.with_defaults sleep:0 | |
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
Hi - this script uses the Sunset version of the Heroku API and will no longer work.