Skip to content

Instantly share code, notes, and snippets.

Created June 19, 2012 17:52
Show Gist options
  • Save anonymous/2955544 to your computer and use it in GitHub Desktop.
Save anonymous/2955544 to your computer and use it in GitHub Desktop.
Heroku Rolling Restarts
class HerokuMaintenance
def self.rolling_restarts
puts "Processing Rolling Restarts"
processes = time_hash[Time.now.hour]
if processes
puts "restarting: #{processes.inspect}"
processes.each { |process| restart_process(process) if process}
else
puts "No restarts scheduled for hour: [#{Time.now.hour}]"
end
end
def self.restart_process(name)
puts "restarting process #{name}:"
heroku_client.ps_restart(ENV['APP_NAME'], :ps => name)
end
def self.time_hash
heroku_ps = heroku_client.ps(ENV['APP_NAME'])
number_of_web_dynos = heroku_ps.select{|h| h['process'] =~ /^web\./}.count
dyno_ids = []
# doing it twice to rotate twice a day
dyno_ids << (1..number_of_web_dynos).to_a
dyno_ids << (1..number_of_web_dynos).to_a
dyno_ids.flatten!
hours_to_run = [1,3,5,7,15,17,20,22,0]
grouped_dynos = dyno_ids.in_groups_of(hours_to_run.length).transpose
t_hash = {}
hours_to_run.each_with_index do |h,i|
t_hash[h] = numbers_to_process_array(grouped_dynos[i])
end
t_hash
end
def self.numbers_to_process_array(array_of_nums)
process_array = []
array_of_nums.each{|n| process_array << "web.#{n}"}
process_array
end
def self.heroku_client
Heroku::Client.new(ENV['APP_USERNAME'], ENV['APP_PASSWORD'])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment