Recently, Heroku switched from offering a cronjobs add-on to offering the Heroku Scheduler add-on. The Scheduler add-on lets you schedule tasks like you are used to with cronjobs. You can run a task every 10 minutes, every hour or every day.
In this post I’ll talk about how to create custom management commands in Django and how you can let Heroku Scheduler perform these commands at set times.
1. Create a management folder with a commands folder in it
Django looks for custom management commands in the
management/commands/ subfolders of you app.
Django will look for all files in the commands folder that don’t start with a underscore. The name of each .py file without a starting underscore is also the name the command will be when running it.
2. Create a Command class that inherits from BaseCommand
In your command_name.py file, create a class Command that inherits from the BaseCommand class. Also don’t forget to import the BaseCommand class.
The action that you want your custom django command to do should be in a
handle(self, *args, **options) method.
As the above code shows, you can capture command line arguments for your custom command. If you don’t need those, you can skip the for loop that loops over the
3. Test if your custom Django command works
Before committing your custom command and pushing it to Heroku, you should test your custom Django command locally.
To do this run:
python manage.py your_command_name
(if your command has additional arguments you can append them at the end)
If your custom command works like you want it to, commit the changes to your repo and push them to heroku.
Now you should check if your new Django management command also works on your Heroku app.
For this, try running the following command:
heroku run python manage.py your_command_name
If all went well, this should succesfully run your custom managent command on Heroku.
Now let’s make sure this command is run at scheduled intervals.
4. Add the Heroku Scheduler add-on to your Heroku app
You can add Heroku Scheduler to you app in two ways:
A) Go to your Heroku Dashboard and select the app you like, then go to add-ons and choose the Heroku Scheduler add-on.
B) Instead of using the Heroku website, alternatively, you can use the following heroku command to add the Scheduler add-on:
$ heroku addons:add scheduler:standard
5. Configure the Heroku Scheduler add-on
Now you need to configure the Heroku Scheduler add-on to run your newly created Django custom management command. To open the Heroku Scheduler, use the following command in your shell:
heroku addons:open scheduler (the Heroku Scheduler site will open). Alternatively, you can go to your your heroku admin site > your app and then select it’s Heroku Scheduler page.
Here you can schedule your custom Django command tasks.
In the leftmost box you now type the following:
python manage.py your_command_name
Next, you should choose at what interval (10 minutes, 1 hour, 1 day) your custom command should be performed. Additionally, if you need, you can set at what time the next run of the command should take place. If you’re ready configuring, press Save.
That’s it! You have created a custom Django management command and scheduled it for repeated execution using the Heroku Scheduler add-on.
Please do note that Heroku will spin up a worker dyno to complete the scheduled tasks. This dyno is prorated per second so you should keep an eye on how much time the task takes if you want to keep costs in check.
If you have any questions, please feel free to post them below.