Skip to content

Instantly share code, notes, and snippets.

@noach-cellogicmobile
Forked from mscottford/heroku.rake
Created July 15, 2012 09:34
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 noach-cellogicmobile/3116069 to your computer and use it in GitHub Desktop.
Save noach-cellogicmobile/3116069 to your computer and use it in GitHub Desktop.
Rake task for restarting a Heroku Web Application
## 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
@tomgallagher
Copy link

Hi - this script uses the Sunset version of the Heroku API and will no longer work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment