You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
Cloud Scheduler for Firebase Functions
Episode 148 written by Jeff DelaneyHealth Check: This lesson was last reviewed on and tested with these packages:
- twilio v3.x
- functions v2.x
Find an issue? Let's fix it
Source code for Cloud Scheduler for Firebase Functions on Github
Firebase developers commonly get stuck asking the question How do I trigger a Cloud Function to run ax X time, or every X time intervals. In the past, the best way to handle this requirement was to use a service like cron-job.org to hit an HTTP endpoint, or deploy your own node app to app engine - both options were not ideal. Thankfully, Google launched the Cloud Scheduler in November 2018 to make this time triggers for cloud functions much, much easier.
Setup a Scheduled Cloud Function Task
I highly recommend using a Pub/Sub trigger for scheduled functions over HTTP because they provide secure authentication out of the box. They are also easier to work with when communicating between multiple GCP services.
A scheduled job simply sends a data payload to an HTTP endpoint or Pub/Sub channel based on the schedule you provide it (1 minute of granularity). The tricky part is defining the schedule as a cron table. You could spend some time learning crontab, but the smarter approach is to use a tool like Crontab Guru to build your scheduler.
Let’s look at the entire process step-by-step for building a time-based trigger for a cloud function.
- Deploy a Cloud Function (HTTP or Pub/Sub).
- Go to the Pub/Sub tab on the GCP console and create your topic.
- Define a cron schedule with crontab.guru.
- Go to the Cloud Scheduler tab and create your task.
Robo-Caller Cloud Function
Below is an example of a Pub/Sub cloud function that uses the Twilio Programmable Voice API to make calls to a phone number.
import * as functions from 'firebase-functions'; |