Running background processes with Firebase Cloud Functions

Tayab Soomro
3 min readFeb 13, 2021

I was working on a freelancing project which required me to create an admin panel with functionalities to control various components in the user model. Considering the urgency of the delivery, the natural choice for me was to set up the project with Firebase. I had previously dabbled with Firebase Cloud Firestore, and Authentication and I had thought this was the extent of the services offered by Firebase. To my surprise, this was just the beginning. I discovered Firebase Cloud Functions, which provide a way for a developer to create RESful APIs, manage buckets, and run cron jobs. I ended up using that and a few other products. The full list of products offered by Firebase is shown below.

Full list of products offered by Firebase. Image captured from https://firebase.google.com/products-build on February 12th, 2021.

In this post, I aim to share an example for which I had to run some background job using Firebase.

This type of web architecture where the back end processes are handled by some other server managed elsewhere is called Serverless computing. Here’s a bit of a boring introduction to Serverless computing.

What is Serverless?

Serverless computing is the structure of computing where the backend processes of an application are handled by a third-party which provides Backend as a Service (BaaS). There are quite a few upshots to this setup, especially for a small-scale application where the financial resources are scarce and running the backend servers in-house becomes challenging. However, depending on the type of services being run on the BaaS, the cost difference might not be as much. Therefore if you are planning to choose the right cloud infrastructure provider, I suggest you do thorough research on the current options and look at the costs associated with each of them.

Let’s get right to the example!

Package the data into a JSON file every 24 hours

The scenario is this –– The users on the application are performing redeem requests which are handled manually by the client. The client would like to get the list of requests that are sent every day in a comma separated values (CSV) format. Therefore, in the background, a CSV file needs to be generated every 24 hours and the client should be able to access this from the web-interface. I created a backend process that generates the file with a specific name, and therefore if such a file exists previously, it is replaced with the newer one.

I used Google’s PubSub functionality for this. I recently learned about this feature after running into the problem of having to generate the file every 24 hours. Briefly, PubSub, a publisher/subscriber model, is a messaging service through which messages can be sent between applications. You can read more about its features here. What it also allows you to do is to schedule tasks using PubSub topics which are carried out by Cloud Scheduler (like a crontab if you know what that is).

Here’s how I used the aforementioned scheduler to create a CSV file every 24 hours:

The schedulefunction accepts as a string parameter that represents an interval in YAML cron format.

Hope that was helpful!

--

--

Tayab Soomro

Bioinformatics • Problem Solving • Coding • Learning • Writing