Build and deploy Python cron scheduler using APscheduler and Heroku

Sourav Kumar
5 min readApr 24, 2022

--

Grab that Amazon deal of the day right from your Phone using your own CRON scheduler !

Do you always feel that you missed that offer or deal from Amazon just by few seconds or minutes ?

Do you feel the need for having personal assistant right in your phone who can notify you of all your important meetings and schedules for the day ?

Do you want to have reminders for important tasks to be done in the day or you don’t want to miss wishing your friend on his/her birthday ?

Answer to all the such questions and many more …… ⌛

I hope you all agree that having some kind of scheduler or service that runs in background i.e. doesn’t interferes in your daily life and yet does it’s job perfectly in all the above situations — that’s what Cron Jobs do.

It’s a way of automating boring and routing chores or tasks which we as developers don’t feel the need of doing it manually. It can be executed at fixed times, dates or fixed intervals or even at different dates / times periodically.

Before Jumping on the cron service, let’s have a glance of the syntax of the cron that is widely uniform across various platforms or technology.

CRON from Wikipedia

If you want to build complex Cron schedules, you might want to look at the best site for that crontab.

Let’s now have a look at the design of the system based on the requirements below —

  • System should be able to grab the latest price of the product you want the deal for and compare with your budget (if it fits your specified range).
  • System should be able to perform the above at fixed regular intervals of few seconds (let’s say 10 seconds).
  • System should be able to notify you on your phone through SMS as soon as it finds the right deal for you.
System Design

prerequisites :

✅ We will be using Rainforest wrapper API which will help us to communicate with Amazon Product API and free us from any hassle of creating accounts on amazon developer portal.
Click here to get Rainforest free API key and copy it somewhere safe.
You should see a similar screen as shown below after initial setup —

✅ We will be using Twilio services which are the best for sending SMS notifications to your Phones within milliseconds.
It’s free to setup and use twilio’s own generated trial number for our developement.
Click here to get auth keys and tokens and copy it somewhere safe.
You should see a similar screen as shown below after initial setup —

✅ We will be using Heroku for deployment using container stack strategy so that it’s easy to get started for anyone because Docker 🐳 is awesome and deployment is super easy on Heroku.
Go ahead and create an app with your favourite name given to it. Here I create “cron-service-amazon”.

👉 Now, let’s write our source code for the CRON service.
Python has a really amazing library for the same utility — apscheduler.

We are going to use BlockingScheduler from apscheduler to write our function to be triggered as a Job which will run at fixed intervals of let’s say 20 seconds (let’s make it a environment variable) .

You see there are two special functions “get_amazon_product_response”, “get_message_for_twilio_SMS”, “send_twilio_notifications” that are called and are yet incomplete. Let’s complete each of them one by one.

Function names are itself descriptive to make it readable and more clear to convey its purpose.

Notice the various environment variables used to mask the sensitive auth keys and token. It’s a good practice to directly inject these sensitive data on runtime and not hardcode anywhere in source code.

Finally combine all the above functions to generate the Cron Job as shown below —

def get_message_for_twilio_SMS........def send_twilio_notifications(message_body):........def get_amazon_product_response():........cron_schedular = BlockingScheduler()@cron_schedular.scheduled_job(trigger="interval", seconds=int(CRON_INTERVAL_SECONDS))def trigger_amazon_product_price_notifications():.................cron_schedular.start()

🔗 Complete source code is hosted here.

👉 Now, it’s time to deploy your application on cloud so that it keeps running in background and performs the Job perfectly. Let’s use the container strategy to deploy our application to Heroku.

Following is a Dockerfile which we will use to deploy to Heroku servers.

Now, we are ready to deploy our application as shown below.

Open your CLI and authentication to Heroku container registry using

heroku container:login

Now, go to the root of your project directory and run the following commands.

heroku container:push worker -a $HEROKU_APP
heroku container:release worker -a $HEROKU_APP

where HEROKU_APP is the name of your created app via Heroku web UI.

Let’s see our app working in production !

You can check your application live logs using

heroku logs --tail -a $HEROKU_APP
case A: found the deal !
case B: not found the deal !
SMS notification on phone

🔗 Check out the complete codebase for this application here.

If you would like to stay connected, follow this account and stay tuned for new Apps building from scratch or new machine learning/deep learning project every weekend.

Watch this space next weekend ! 👋

--

--

Sourav Kumar

Deep Learning 💻| Machine Learning 📊| Full stack Web development 🌐| cosmos lover 👨‍🚀