Analytics Vidhya
Published in

Analytics Vidhya

How to schedule a python project/script in Heroku with zero cost

Background

In this COVID-19 pandemic period, the popularity of Crypto coins heavily increased among society. The majority of society isolated in their homes and most of them tend to follow online earnings. Ten out of nine people will be interested in cryptocurrency trading because popular coins’ prices increased higher and higher day by day.

I am working on a project these days that are related to cryptocurrency trading platforms. My company already had created a web crawler that capable of crawl crypto coin tables from the reputed crypto trading platform when they assigned me. After I am in, my supervisor assigns me a task to crawl crypto coin table data every five minutes and store it in MySQL tables (Every five minutes, there should be a new table). After that, deploy the project into Heroku and make it running without human interactions.

I have searched the same topic as assigned to me and found the medium post (https://medium.com/analytics-vidhya/schedule-a-python-script-on-heroku-a978b2f91ca8) written by Mr. Sathindu Kavnath. After getting some inspiration from his story, I followed a different path and found a better way to complete my task with zero cost. Let’s start defining my observation as a tutorial.

Step 1 (Create a GitHub repository)

Before we start, you have to have a Heroku account and a GitHub account. It is better to download and install Heroku CLI also.

For python developing I have used PyCharm. In your python project there must be three important files.

  1. Procfile
  2. Requirements.txt
  3. Runtime.txt
  4. And of course, your python script.

When creating repository in GitHub, you can create it as public or private. It will not affect to your Heroku deployment.

Step 2 (Defining necessary files)

  1. Python script

I have used simple Hello world python script in below and I have schedule to print Hello World in every 1 minutes. In my case, I have used “start” as my python file name, but you can use any name you want.

2. Procfile

Simply, Procfile use to store commands that act like dyno on the Heroku platform. In Heroku dynos work as containers. You can activate or deactivate dyno as you wish. When you activate a dyno, it will execute the command that you defined in Procfile. There are two main process can see in Procfile.

  1. Web -: Use when deploying web apps or HTTP request related scripts.
  2. Worker -: Use when deploying normal scripts (In my case I have used worker).

Remember, Procfile is not a text file. It is just a file. In my case, my python file’s name is “start”. Therefore, I have used the command “python start.py” to execute my code.

3. Requirment.txt

In here, we can define our libraries with their versions that we used in our project. In my case I have used one main library called schedule.

4. Runtime.txt

In here, we can define our python version that we are going to use in our project. In my case I have used “python 3.8.8” but you can use any version according to your choice.

Step 3 (Deploy the script to Heroku)

After completing above scenarios, you can add your project to your GitHub.

After that we can now move into Heroku. After setting up your account, you can see the interface like below. In there, select “New” and then select “create new pipeline.”

When creating pipeline, you have to give name to your pipeline and connect GitHub repository with the pipeline. When you give a name to your pipeline you must enter only Lowercase letters. In my case, I have used “medium” as my pipeline name. After that you have to give your repository name and Heroku display the project and you can click connect to add connection between GitHub and Heroku.

Now click the “Create pipeline” button.

Now you can see the interface of you pipeline. Now we are moving to stage our project. Before we start, you need to enable “review app” (Review apps can be created for pull requests opened on GitHub) feature in settings.

Without checking any point, just click the “Enable Review Apps

Now, go to pipeline again and click on “Add app” button.

Now give a name to your app according to instruction. I have given my app name as “medium-m

After creating app, you can now deploy it. Just follow as mine.

Now click on “view log” to view how the deployment process is working and check whether is there any problem?

You can see our Procfile, requirement.txt, and runtime.txt all are executed in log.

Step 4 (Execute the app)

Before executing the app, I have previously asked you to download and install Heroku CLI. Now let’s try to move into Heroku CLI a bit. In there we can watch the live logs when executing our app. In there we can view results, execution errors, Etc. Follow as me.

  1. Type “heroku login”. Then follow given instruction in cmd.

2. Next, you can type “heroku logs –app <your app name> — tail”. In my case I have used “heroku logs –app medium-m — tail”. You can see app deployment log is already there.

Now we move into our dynos. To run the app, we have to activate our dyno. Therefore, do same as me.

After clicking “medium-m”, navigate to “Resources tab and you can see our dyno is not activated yet.

To activate it click on the pen icon near to red circled icon and click it one time then you can see it will be colored. Then click confirm button to execute our script.

Now let’s see our command prompt to see our results.

You can see our script running successfully. Script executes exactly in every 1 minutes. If you want to stop the script execution just simply turn off the dyno.

Project link -> https://drive.google.com/drive/folders/1cAPINhZb5nzpsysziqtljePyhCw2g8xv?usp=sharing

Conclusion

Likewise, you can deploy any python script (with database connection, without database connection) on Heroku. Remember if you are willing to connect a database with your project do not use Heroku database add-ons because those providers provide costly service. I recommend, use AWS RDS instead of other cloud databases because we can do lot of things AWS free tier.

Most of Heroku users are facing time zone problem when using Heroku. If you are working on region-based project and you are unable to generate according to your time zone just follow bellow steps.

I hope this tutorial will help when you achieve your goals in future. I will come up with another useful article later. Thank you!

--

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Recommended from Medium

Azure K8S and Sentry (Part 1)

How do I set up an email notification for security group changes in the Account

Authentication Techniques for APIs

‘Android Things’ Hack Day: Hot in The Office

Bash(and others) things that i use every day

CS373 Spring 2021: Ashish Kanjiram

My journey has started to become a DATA ANALYST.

The release is here

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kalana Eranda Jayasuriya

Kalana Eranda Jayasuriya

Data Analyst at Kodez | Former Research Analyst at CoinGuru | Data Science Undergraduate from Sri Lanka Institute of Information Technology

More from Medium

Data Structure in Python

🎮 NEW 2021 | How To Beat Pokémon SQL Version | SQL Interview Practice Guide

Why and how I tried to make a programming language.

SAS Programming: Zero My Hero — Using the Z Format with Numbers