Created
April 4, 2012 12:18
-
-
Save mscottford/2300740 to your computer and use it in GitHub Desktop.
Rake task to restart one heroku web instance every 10 minutes
This file contains hidden or 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
# Add `gem 'heroku-api', :git => 'https://github.com/heroku/heroku.rb.git'` to your Gemfile | |
# Set the APP_NAME environment variable to the name of the Heroku app you wish to control. | |
# Set the HEROKU_API_KEY environment variable to the API key found on the Heroku 'My Account' page. | |
# You can now create a scheduler instance that runs `bundle exec rake heroku:web:restart` to automate | |
# restarting workers. The task will select a different web instance every 10 minutes, based on the number of instances that are running. | |
namespace :heroku | |
namespace :web do | |
task :restart do | |
heroku = Heroku::API.new | |
response = heroku.get_ps(ENV['APP_NAME']) | |
web_processes = response.body.map {|item| item['process'] }.select { |item| item =~ /web/ } | |
which_process_index = HerokuHelpers.which_web_process_to_restart?( | |
web_processes.length, | |
DateTime.now) | |
process = web_processes[which_process_index] | |
puts "Restarting #{process}" | |
heroku.post_ps_restart(ENV['APP_NAME'], 'ps' => process) rescue nil | |
end | |
end | |
end |
This file contains hidden or 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
require 'time' | |
module HerokuHelpers | |
def self.which_web_process_to_restart?(workers, time) | |
((time.hour * 60 + time.min) / 10) % workers | |
end | |
end |
This file contains hidden or 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
require 'spec_helper_no_rails' | |
require 'heroku_helpers' | |
describe HerokuHelpers do | |
context '.which_web_process_to_restart?' do | |
context 'with 4 workers' do | |
start = Time.parse('00:00:00') | |
144.times do |index| | |
time = start + 60 * 10 * index | |
expected_result = index % 4 | |
it "should return #{expected_result} at #{time.strftime('%T')}" do | |
HerokuHelpers.which_web_process_to_restart?(4, time).should == expected_result | |
end | |
it "should return #{expected_result} at #{(time + 2).strftime('%T')}" do | |
HerokuHelpers.which_web_process_to_restart?(4, time + 2).should == expected_result | |
end | |
end | |
end | |
context 'with 3 workers' do | |
start = Time.parse('00:00:00') | |
144.times do |index| | |
time = start + 60 * 10 * index | |
expected_result = index % 3 | |
it "should return #{expected_result} at #{time.strftime('%T')}" do | |
HerokuHelpers.which_web_process_to_restart?(3, time).should == expected_result | |
end | |
it "should return #{expected_result} at #{(time + 2).strftime('%T')}" do | |
HerokuHelpers.which_web_process_to_restart?(3, time + 2).should == expected_result | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for sharing