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:
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.
1👍
It’s not built into the platform but should be pretty easy to implement via scheduler and using your API token.
- Have a url that accepts all characters
- Django select related in raw request
- Many to many relationships with additional data on the relationship
- Django – Delete file from amazon S3
- Example when request.POST contain query string in django
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.