[Part 2] Automating boring work with Heroku

Tram Ho

In the previous section , I made a simple bot to post to my Fan Page. In this section, I will continue to use Heroku to deploy a bot and not spend any cost at all.

What is heroku?

Heroku is a PaaS (Platform as a service) used to help you deploy applications without spending too much effort on miscellaneous configurations. For those of you who do not know much about Devops, the code then deploying is a big problem. With Heroku, simply, the code is finished and deployed only, no need to care about https, docker and all. In return, the cost is quite expensive compared to a normal VPS. However, Heroku has provided you with the Free package to use, somewhere it seems that 5 apps deploy to Heroku must, plus 550 hours dyno hours.

So what is Dyno Hours?

Each default Heroku account is provided for 5 apps to deploy with a total of 550 Dyno Hours.

Dyno Hour is the total online time of all 5 apps when it is awake after deploying to the system.

That means you do anything directly on the Website it also counts on Dyno Hour. In general, you can connect to the app and it counts Dyno, no matter if you use Terminal or do anything.

Examples are as follows:

You deploy a website to Heroku, and people can access your website. Your website can live 550 hours a month (Range 22, 23 consecutive days). After 550 hours, your site will automatically shut down, and wait until next month, after the Dyno hours are reset.

Note that Dyno Hours is a cumulative sum of all the apps in your account, so if you use 5 apps, the Dyno hours will run out very quickly.

However, if you enter the Credit Card, it will extend up to 100 apps and 1,000 Dyno hours, enough for you to write a small bot comfortably. Because a bot only runs a few hours, and a run only takes a few minutes, with 1,000 hours Dyno is comfortable using the hen, no worry about Dyno Hours. When Dyno Hours reaches 70%, it will send you an email so you don’t have to worry about running out of time.

Start deploying

Create a Heroku account

This step is simple, go to Heroku and create a normal Developer account. If you want to add a Credit Card to get more apps and more Dyno hours, then you add in Account Settings -> Billig -> Add Credit Card is done.

Download Heroku CLI

Installing this is also simple, nothing shitty fuck at all. Just go here and download it. I use Linux so I just type

Deploy using Heroku CLI

After having those two things on your Dashboard, click Create New App to create a new App only. Then go to your project under local, start deloying up. Heroku CLI uses exactly the same as the git statements so there’s nothing to say. The first is to log into Heroku CLI

Then push it on the remote of Heroku

After that, it will push up, leaving her miscellaneous settings, then Heroku takes care of it, sits and waits.

Note: Often when deploying a NodeJS system, people often instruct to add a file called Procfile to auto start server. However, here I wrote a bot, which can only be run through CLI, so this file is not needed.

After deploying, then start using it. Go to More, select Run Console, then type in bash to access the terminal.

Screenshot-from-2019-12-25-18-17-26

Once done, type as node index.js see if it returns the correct result. The following is the standard mother to adjust it

Screenshot-from-2019-12-25-18-17-57

Use the Scheduler to automate

Now there is only one final step to install an AID On of Heroku named Heroku Scheduler to schedule, to make it run every hour. You go here and then click Install Heroku Scheduler, select the plan as Free and install it into your app.

Heroku

After finishing, you go back to your app’s Dashboard page, it will show the Heroku Scheduler there.

listen-to-this-nguyen-47 --- Heroku

Click Configure Add On and then click Add Job to schedule only. For example, here I will let it run once every hour.

listen-to-this-nguyen-47 --- Scheduler --- Heroku

That’s it, now just wait and it will automatically run the script for you.

Conclude

After deploying, every hour, Heroku will automatically run that command once to post on Facebook. Based on this mechanism, I can perform other similar tasks, such as automatically sending emails every day, automatically backing up data every day or automatically enabling script scraping data` every day. You can customize improvisation to suit your needs.

You can see the results in their Page http://fb.com/listenthisawesomesong . I have been there for a whole month, but I kept posting it loudly, no need to move my arms and legs: v

Currently I am learning to write Blog at https://htknguyen.com/ . If you are interested, then visit my blog to hear me slashing the wind up in the perspective of a leprous Developer. Shoot heart

Share the news now

Source : Viblo