Skip to content

Instantly share code, notes, and snippets.

@mataki
Created December 2, 2010 06:33
Show Gist options
  • Select an option

  • Save mataki/724890 to your computer and use it in GitHub Desktop.

Select an option

Save mataki/724890 to your computer and use it in GitHub Desktop.

Revisions

  1. mataki revised this gist Dec 9, 2010. 1 changed file with 22 additions and 6 deletions.
    28 changes: 22 additions & 6 deletions heroku_autoscalling.rb
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,39 @@
    =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"

    app_name = "app-name"
    new_relic_license_key = "new_relic_license_key"
    # setup config
    app_name = "app_name"
    license_key = "license_key"

    NewRelicApi.license_key = new_relic_license_key
    # get NewRelic data
    NewRelicApi.license_key = license_key
    puts values = NewRelicApi::Account.find(:first).applications.first.threshold_values
    puts threshold_value = values.detect{ |v| v.name == "Apdex" }.threshold_value
    threshold_value = values.detect{ |v| v.name == "Apdex" }.threshold_value
    puts "threshould_value: #{threshold_value}"

    # heroku = Heroku::Client.new('email', 'password')
    # 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
    @@ -30,4 +46,4 @@
    else
    (current + threshold_value)
    end
    puts heroku.set_dynos(app_name, next_dynos)
    puts "set: #{heroku.set_dynos(app_name, next_dynos)}"
  2. mataki created this gist Dec 2, 2010.
    33 changes: 33 additions & 0 deletions heroku_autoscalling.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    require "rubygems"
    require 'heroku'
    require "heroku/command"

    require "newrelic_rpm"
    require "new_relic_api"

    app_name = "app-name"
    new_relic_license_key = "new_relic_license_key"

    NewRelicApi.license_key = new_relic_license_key
    puts values = NewRelicApi::Account.find(:first).applications.first.threshold_values
    puts threshold_value = values.detect{ |v| v.name == "Apdex" }.threshold_value

    # heroku = Heroku::Client.new('email', 'password')
    heroku = Heroku::Command::Auth.new({}).client

    current = heroku.info(app_name)[:dynos].to_i
    puts "current: #{current}"

    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 heroku.set_dynos(app_name, next_dynos)