schedule number of web dynos by time of day
Asked Answered
P

4

16

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...

Phrixus answered 14/8, 2012 at 9:15 Comment(0)
D
16

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.

Doolittle answered 14/8, 2012 at 22:46 Comment(3)
This answer was incredibly detailed. Thank you so much for investing all this time. It is very much appreciated... naturally I accept your answer as the answer. :)Phrixus
Thanks! :) I'm glad it helped. Who knows maybe some other people will find it useful too ;)Doolittle
My colleague asked Heroku support about this and they said this would work but did not recommend using Heroku Scheduler as it is a "best-effort" service, meaning that it is not guaranteed to run or might run more than once.Jimmie
R
1

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

Rafaelof answered 14/8, 2012 at 11:0 Comment(0)
U
0

Easy to do now with heroku addon plugin crontogo https://elements.heroku.com/addons/crontogo

Note: This is a paid plugin, and available only in US/Europe regions as of Oct 2021

Refer https://crontogo.com/blog/how-to-schedule-heroku-cli-commands/

Setup 2 jobs

1st Job “Dyno scale down” :

Cron expression: 0 18 * * 1-5

Command: heroku dyno:scale web=1:Standard-1X

This job will run on weekdays (Monday to Friday) at 6pm and scale the app down to 1 Standard-1X dyno.

2nd Job “Dyno scale up” :

Cron expression: 0 9 * * 1-5

Command: heroku dyno:scale web=2:Standard-1X

This job will run on weekdays (Monday to Friday) at 9am and scale the app up to 2 Standard-1X dynos.

Unionism answered 13/10, 2021 at 17:12 Comment(0)
D
0

You can also do this via the heroku scheduler, which is free compared to cron-to-go. Just fill in [your-app-name] and [your-token]. The token can be generated via the command-line-interface and the following command: heroku authorizations:create

curl -n -X PATCH https://api.heroku.com/apps/[your-app-name]/formation -d '{ "updates": [ { "quantity": 1, "size": "standard-1X", "type": "web" } ] }' -H "Content-Type: application/json" -H "Accept: application/vnd.heroku+json; version=3" -H "Authorization: Bearer [your-token]"
Desorb answered 4/1 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.