Skip to content

Instantly share code, notes, and snippets.

@samsm
Created November 29, 2011 15:51
Show Gist options
  • Save samsm/1405275 to your computer and use it in GitHub Desktop.
Save samsm/1405275 to your computer and use it in GitHub Desktop.
"Auto" Scale Heroku Cedar instances with rake tasks
I think this is a nice option for apps that are mostly used during particular hours and aren't really ready
for a more intense auto-scaling thing. Hirefire (https://github.com/meskyanichi/hirefire) likely does a
lot more, but I didn't really understand how it worked and don't really need something that fancy right now.
I suspect it requires at least one worker itself, but I don't really know.
Add an API key to your app's Heroku config
heroku config:add HEROKU_API_KEY=your_key (from https://api.heroku.com/account )
Add the above rake task. Change 'YOUR-APPS-NAME' to indigo-butterfly-77 or whatever your app's name is.
Put some scale instructions in Heroku scheduler:
rake scale[web=2,worker=1] at 6:00 am
rake scale[web=1,worker=1] at 6:00 pm
rake scale[web=0,worker=0] at 1:00 am
rake scale[web=1] at 5:00 am
This was an easy little thing to reduce processor hours.
I'm thinking about using it to set web=0 on my free apps at night,
so I have some free hours to burn on workers/etc during the day.
desc "Scale up/down dynos"
task :scale, :arg1, :arg2, :arg3, :arg4, :arg5, :arg6, :arg7, :arg8 do |t, args|
args.entries.collect(&:last).each do |scale_instruction|
process_type, number_of_instances = scale_instruction.split('=')
scale(process_type, number_of_instances) if number_of_instances =~ /\A\d+\Z/
end
end
def scale(process_type,number_of_instances)
curl_command = <<-eos
curl -H "Accept: application/json" \
-u :#{ENV['HEROKU_API_KEY']} \
-d "type=#{process_type}" \
-d "qty=#{number_of_instances}" \
-X POST https://api.heroku.com/apps/YOUR-APPS-NAME/ps/scale
eos
system(curl_command.gsub(/\s+/,' '))
end
@aguywithanidea
Copy link

To allow this rake task to be deployed to both 'staging' and production apps on Heroku, we changed the line:

-X POST https://api.heroku.com/apps/YOUR-APPS-NAME/ps/scale

to read

-X POST https://api.heroku.com/apps/#{ENV['HEROKU_APP_NAME']}/ps/scale

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