This article explains how to deploy a simple python script on Heroku in order to automatize a task online.

Heroku provides an app called Heroku Scheduler. It is free but it only runs scheduled tasks every 10 minutes, every hour, or every day. But what if we need to run script every minute for example? In order get a full control on our automated task, we will learn how to create a customized cronjob and deploy it to Heroku.

Create the files

Set the local anvironment and install needed packages:

# Create virtal environement folder inside the project and activate it
python3 -m venv venv
source venv/bin/activate
# Install the needed packages
pip install apscheduler

requirements.txt

Generate a requirements.txt file for our dependencies:

pip freeze > requirements.txt

script.py

Create a file called script.py:

cat > script.py

Past the folowing code in the terminal (finish by tapping Enter):

from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
@sched.scheduled_job('interval', minutes=3)
def timed_job():
   print('This job is run every three minutes.')
   
@sched.scheduled_job('cron', day_of_week='mon-fri', hour=17)
def scheduled_job():
   print('This job is run every weekday at 5pm.')
   
sched.start()

Procfile

Create a file called Procfile in the project's root folder and add this line into it:

echo "clock: python clock.py" > Procfile

runtime.txt

Get the last supported runtime on this page and past it into a runtime.txt file located in the root directory of your app:

echo "python-3.10.7" > runtime.txt

Deploy the app on Heroku

It is time to push the application to Heroku.

Download and install the Heroku CLI. If you haven't already, log in to your Heroku account and follow the prompts to create a new SSH public key.

heroku login

Create a repository

Create a unique name for your Web app. For this tutorial we will call our app heroku-cronjob-example:

heroku create heroku-cronjob-example

You should be prompted with 2 links: 

  • https://heroku-cronjob-example.heroku.com is the url of your app.
  • https://git.heroku.com/heroku-cronjob-example.git is the url of the heroky repository

Set the Buildpack

Builpacks are a set of rules that tells how Heroku should deploy the app. It is necessary for a Pyton app to tell Heroku to use the Buildpack dedicated to Python. To do so, run this command:

heroku buildpacks:set heroku/python -a heroku-cronjob-example

Push the app to Heroku

Initialize Git, set the remote repository to the one you just ceated:

git init
heroku git:remote -a heroku-cronjob-example

Create a .gitignore file and add the venv folder to it (to prevent Git to commit the files inside it):

echo "venv" > .gitignore

Do your first commit and push your project to Heroku:

git add .
git commit -m "first commit"
git push heroku master

Launch the cron job

Finaly, to launch the script, run this commande in terminal:

heroku ps:scale clock=1

Verify that the deployment was successful

It is necessary then to verify that everything is working properly. To do so, below are some terminal commands  that may come in useful.

  • Check the logs:
heroku logs --tail
  • Navigate through the app files using the heroky CLI:
heroku run bash -a heroku-cronjob-example # Open the CLI for this repository
ls # List files
cd path_to_folder # Navigate through the folders
cat path_to_file # Show the content of a file in the terminal
  • Exit the CLI pressing Ctrl+D

To go further:

⇐ Terminal Commands (CLI) for...Python: Virtual Environment... ⇒
Image
Author of this website. I am passionate about programming and web design since the age of 12. After a stint in architecture and entrepreneurship, I came back to my original passion at 30 years old and became a self-taught web developer in less than 2 years.

Leave a Comment