On this blog, you are used to reading articles about Android, Kotlin, JUnit, etc, but today I want to talk about: Cloud Functions and Cloud Scheduler, two services that you can find inside the Google Cloud Services suite.
Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. It’s server-less so you don’t need a remote server to host them and you pay only the requests done, it means that they are cheap because you don’t need to pay a server hosting day by day. About pricing I invite you to see the website because under a limit they are free.
To speak about Cloud Scheduler we have to take a leap into the past…
A long time ago in a dirty laboratory was invented the Cron, a job scheduler that allows UNIX users to schedule the execution of a job/script in a specific time with a programmed repeat. It’s something easy but in many cases very useful, for example, if you want to ping a service, or use an API for fetching specific information (for example if your online supermarket has a slot for shipping your groceries), or make a backup from a database to another.
Google has implemented the modern version of this service but with the same principles.
The recipe: what we need
Fortunately, the implementation is quite simple if we use Firebase, because it wraps many parts of setup and configuration, we need only to think about the functions and whats we want to do.
To do that we need our
Terminal and the latest version of
NodeJs downloadable from http://www.nodejs.org.
Start a new Project
We can start by opening our terminal and install the latest version of
firebase-tools with the following command:
% npm i -g firebase-tools
Firebase-tools is a script that allows you to create/deploy the firebase functions, and also has an emulator and other stuff to test your projects.
After the installation, we need to create a folder for our project
% cd test-cron-on-firebase
and runs the init method inside the project folder
% firebase init
during the setup we need also to choose what firebase feature we want to add and the firebase destination project:
? Which Firebase CLI features do you want to set up for this folder? Press Space to select features, then Enter to confirm your choices.◯ Database: Deploy Firebase Realtime Database Rules
◯ Firestore: Deploy rules and create indexes for Firestore
❯◉ Functions: Configure and deploy Cloud Functions
◯ Hosting: Configure and deploy Firebase Hosting sites
◯ Storage: Deploy Cloud Storage security rules
◯ Emulators: Set up local emulators for Firebase features
in my sample I will use only the firebase functions, but if you want to use a database or something else you should remember to flag it.
First, let's associate this project directory with a Firebase project.You can create multiple project aliases by running firebase use --add,but for now we'll just set up a default project? Please select an option: (Use arrow keys)❯ Use an existing project
Create a new project
Add Firebase to an existing Google Cloud Platform project
Don't set up a default project
At the end of the procedure we should have a file tree like below:
Create my first firebase functions
We need to modify the
functions/index.js to write our first firebase function, the first example is only to understand how works an HTTP function, the next step will be how to define it inside a job.
In this example, I want to create a function that calculates how many days are left to the Bitcoin Halving 2020, you can see the implementation below:
calculateHalving is the function name and it will be the path inside the firebase functions HTTP URL, indeed if we run:
% firebase deploy
we can see to URL to reach our firebase function
If we want to test it locally, we need only to run
How schedule a Firebase Function
Now, if we want to know how many days are left to the Halving we need only to contact that URL, but…if we want to receive an email with this information every day at 10.00 AM? We can schedule it.
We have changed only the function definition by replacing the
functions.pubsub.schedule() ,the function argument is the cronjob configuration based on the Cron Job standard that you can find here.
I have not implemented the sending of email because there are many other articles about it and many other npm libraries.
The real Magic is that after
firebase deploy the firebase-tools will publish the function and also it will configure the Job Scheduler automatically. In fact, after the deploy, if we open the Google Cloud Scheduler console we may found the list of all schedulers we have defined.
I think this is a cheap way to create a set of recursive small functions, the limit is only the imagination. At the end of the article you can find the source code.