Project to Calculate COVID Moving Average — Heroku Deployment[Part 3]

Fernando Raposo
5 min readJan 21, 2021

This is a series of posts, all related to the same project, but each one is focused on specific issues.

The literature always says the line: “You can’t control what you can’t measure”. And it is so true! Nowadays Continuous Integration (CI) plays an important role on measurement and also on controlling applications. There are many players selling cloud services such as AWS, Google Cloud, Microsoft Azure and Heroku, just to say a few.

This part covers project deployment. Previous parts covered:

Part 1: It covers Spring Batch topics and general system modelling and Moving Average concepts;

Part 2: It covers Firebase topics detailing how to use Firebase to store JSON documents related to COVID information;

Heroku is a platform as a service (PaaS) which enables developers to build, run, and deploy applications entirely to the cloud. It supports Node, Ruby, Java, Scala, PHP and more. In comparison to other platforms, such as AWS and Azure, Heroku is quite straightforward to learn and it allows free app deployments after just a few clicks.

So, our objective is (1) to deploy the COVID project to Heroku and; (2) to create a mechanism to run it everyday so that our data is allways up to date.

1. Heroku Account (Using the Free Dynos)

Go to heroku.com and create an account. You may have to insert some credit card details but do not worry, we will only use the free options or, as Heroku says, the free Dynos. But wait… What is a Dyno?

A Dyno is the name Heroku gives to a container. Containers are the building blocks of today’s cloud services. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing. It can also be defined as a type of virtualization. Those concepts of containers, virtualization, etc. are all related to Docker. You can find more about it here.

After logged in, go to “Account Settings” and choose Billing tab on Free Dyno Usage section. See bellow:

Free Dyno Usage

The picture shows your applications free Dyno usage. Notice that Heroku provides 1,000 free dyno hours per month, which is quite good!

2. Project Deployment to Heroku

Go to Heroku dashboard, click on New and select “Create new app”

CHoose Create new app

Next, choose a name you like to the project (it needs to be unique, but Heroku will check it for you). It’s ok to keep the region United States. For now avoid clicking on “add pipeline”. After that, click on “Create app”.

After the project is created, it is necessary to run the code versioned into GitHub on Heroku in order to deploy it. This requirement is achieved by connecting Heroku to GitHub. Go to your recently created projects, and on the deploy tab click on Connect to GitHub. After the permissions are granted, search the repository where the code is and click on “Connect”.

Connecting Heroku to GitHub

The next topic is “Automatic Deploys”. Click on “Enable Automatic Deploys” choosing master branch to deploy. This means that after every push on GitHub, a new Deploy will be generated. Remember not to check the option “Wait for CI to pass before deploy”. Finally, click on “Deploy Branch” on “Manual Deploy” topic to deploy the project. You will see the project being deployed on an area near .

Deployment

If everything turns green after clicking on “Deploy Branch”, your project is successfully ON.

A Successful Deploy

4. Adapting Project to Heroku

Before running the project on Heroku, it is required a description about how to run the JAVA Project created to collect and store COVID Measurements. This is done using a Procfile. Heroku apps must include a Procfile that specifies the commands which are executed by the app on startup.

Procfile creation

So go back to root folder of the JAVA Project and create a new text file called Procfile (without file extension). And add the following text:

worker: java -jar target/spring-batch-rest-0.0.1-SNAPSHOT.jar

This command means that to run the project (after deployment) Heroku must call that jar file (spring-batch-rest-0.0.1-SNAPSHOT.jar) which resides on target folder. Save it and push it. After that, go to the project on Heroku console, and on Resources tab check if the command written on the Procfile is there. If it is, we’re done!

Procfile ready

5. Heroku Scheduler

Coronavirus data is updated by public entities daily. As a result, we need to run on Heruku our JAVA program once a day, in order to store new data on Firebase. Obviously, it can be done automatically by using a Scheduler on Heroku.

Go to Resourses tab on the project and on Add-ons area type “scheduler” and choose “Heroku Scheduler” option.

Heroku Scheduler

Accept Standard Free Plan and press the “Submit Order Form” button. Next, go back to Overview tab, and click on Heroku Scheduler option. This will open a new tab on browser.

Scheduler Configuration

On the new browser tab, click on Create job. A form will appear to set job interval (every day, every hour and so on…). A specific hour every day is enough (maybe 03:00 A.M every day is fine…). Finally , on Run Command section, add the command you set on Procfile (java -jar target/spring-batch-rest-0.0.1-SNAPSHOT.jar) and click on Save Job.

Job Interval set up
Job defined

For testing purposes, before setting to 3 A.M, set for a minute or two in advance. The expected results should be Firebase updated with new data.

6. Conclusion

The complete code can be downloaded here. I’m considering the development of a forth part presenting charts plotting the movable average.

Thank you.

--

--

Fernando Raposo
Fernando Raposo

Written by Fernando Raposo

Computer Scientist, MSc UFPE — JAVA, PL/SQL, Angular, Python, Software Testing