Skip to content

Instantly share code, notes, and snippets.

@mataki
Created December 2, 2010 06:33
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save mataki/724890 to your computer and use it in GitHub Desktop.
Save mataki/724890 to your computer and use it in GitHub Desktop.
Auto scalling dynos on heroku using NewRelic
=begin
Need to install gems heroku, newrelic_rpm
$ gem install heroku newrelic_rpm
Set your apps setting
app_name : heroku's app_name of auto scaling
license_key : NewRelic api key. You can get heroku's NewRelic admin console. "App setting" and "Agent configuration"
execute with cron every minutes
$ ruby ./adjust_dynos_with_newrelic.rb
=end
require "rubygems"
require 'heroku'
require "heroku/command"
require "newrelic_rpm"
require "new_relic_api"
# setup config
app_name = "app_name"
license_key = "license_key"
# get NewRelic data
NewRelicApi.license_key = license_key
puts values = NewRelicApi::Account.find(:first).applications.first.threshold_values
threshold_value = values.detect{ |v| v.name == "Apdex" }.threshold_value
puts "threshould_value: #{threshold_value}"
# get Heroku data
heroku = Heroku::Command::Auth.new({}).client
current = heroku.info(app_name)[:dynos].to_i
puts "current: #{current}"
# set Heroku dyno
next_dynos = case threshold_value
when 0
1
when 1
if current > 1
(current.to_i - 1)
else
1
end
else
(current + threshold_value)
end
puts "set: #{heroku.set_dynos(app_name, next_dynos)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment