Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

schedule number of web dynos by time of day

Is there a way to use the Heroku scheduler to start and stop web dynos for specific periods of the day? Like say during business hours 2 dynos and at night only 1 dyno?

I really would like to avoid putting the normal user/pass credentials into the app itself, so I'm looking for a secure way to do this (apart from doing it manually each day for each app). Using the "heroku ps:scale web=2" directly would naturally be nice but as far as I know this is not supported.

Thanks for any feedback in advance...

like image 300
khaos Avatar asked Aug 14 '12 09:08

khaos


People also ask

How do I check my dyno hours?

Determining your free dyno hours You can view the amount of free dyno hours remaining by using the CLI. You can do this by running heroku ps on one of your free apps. Alternatively, you can also view this on Dashboard's billing page, which is refreshed daily to display the updated amount.

How do I get more dynos in Heroku?

From the Heroku Dashboard, select the app you want to scale from your apps list. Navigate to the Resources tab. Above your list of dynos, click Change Dyno Type . Select the Professional (Standard/Performance) dyno type.

What is a web dyno?

Web: Web dynos are dynos of the “web” process type that is defined in your Procfile. Only web dynos receive HTTP traffic from the routers. Worker: Worker dynos can be of any process type declared in your Procfile, other than “web”. Worker dynos are typically used for background jobs, queueing systems, and timed jobs.

Does Heroku scale automatically?

Autoscaling. Heroku enables you to automatically increase the number of web dynos needed to meet your specified 95th percentile response time threshold. Based on your app's existing throughput, the autoscaling feature removes the need to anticipate traffic spikes.


2 Answers

You can scale heroku dynos on a schedule by creating a script that uses the Heroku API. You then make an entry in your Procfile and call it via the Heroku Scheduler add-on. Here's how:

First you'll need to add the 'heroku' python module to your requirements.txt:

heroku==0.1.2

Next, create a config var that contains your API key, so your script can use the API.

heroku config:add HEROKU_API_KEY=your_api_key_string

You can find your API key on your heroku account page.

Now you'll be able to write a python script that scales your dynos. Here's a very basic script that accepts the number of dynos as a command-line argument.

import os
import sys

import heroku

"""Scale heroku web processes using the heroku python API."""

# you may want to add better argument processing, use argparse, etc.
dynos = int(sys.argv[1])
cloud = heroku.from_key(os.environ.get('HEROKU_API_KEY'))
app = cloud.apps['your_app_name']

try:
    # you may want to add a maximum dyno check here to prevent costly mistakes ;)
    webproc = app.processes['web']  
    webproc.scale(dynos)

except KeyError:
    # note: scaling to 0 dynos or attempting to scale up if 0 web dynos exist
    # both throw this error. Make sure you have at least one dyno.
    print >> sys.stderr, "Could not scale web processes - are there 0 web dynos running?"

Then you can either define your entire task inside the Heroku Scheduler web page, or define it inside your Procfile and call the Procfile process name from the web page. I prefer the latter, as it makes it easy to update or change the process without having to log in to heroku's website.

So, create entries in your Procfile:

scale_up: python scale.py 2
scale_down: python scale.py 1

And then schedule them:

[image]

And voila! Your dynos will now scale up or down at the specified time of day.

--

Note that once you have created a scheduled task on the Scheduler web page you can't edit the time of day it runs, but if you create a new task you can set the time day and then delete your old task.

Note 2: The heroku python API seems to throw a KeyError if you try to scale down to 0 dynos, or if you try to scale up if 0 web dynos currently exist. To avoid both, just don't scale down to 0 dynos.

like image 98
culix Avatar answered Oct 26 '22 16:10

culix


It's not built into the platform but should be pretty easy to implement via scheduler and using your API token.

like image 22
John Beynon Avatar answered Oct 26 '22 18:10

John Beynon