How to Build a Scheduled Task Runner with Firebase Cloud Functions

TL;DR
You can build a dynamic task runner by combining scheduled Firebase Cloud Functions with Firestore documents that define what worker runs, when it runs, and what data it receives. A Pub/Sub function checks every 60 seconds for due jobs, while Firestore timestamps keep execution times consistent across clients. Read on for the required task fields, cron setup, execution limits, and cost considerations.
Transcript
yesterday you may have seen a video about building an industrial scale Robo collar I decided to take that video down and destroy all the physical copies because even though it's an obvious joke it's not the right way to be using those tools but the good news is that I recut that video and it keeps the most important part intact which is building a ... Read More
Key Insights
- ❓ Utilizing Pub/Sub functions and Firestore timestamps enables precise scheduling of background tasks.
- 🏃 The dynamic task runner system built with Firebase Cloud Functions supports user-defined job scheduling.
- 🧑⚕️ Worker functions with options provide flexibility for passing dynamic data to backend functions.
- 🏃 Training on Pub/Sub scheduling and Firestore data structuring enhances task runner efficiency.
- 👻 The task runner system allows for concurrent execution of multiple scheduled jobs efficiently.
- 🏃 Deployment and monitoring of the task runner ensure seamless operation and task execution.
- 🧑⚕️ Enforcement of worker functions returning promises ensures uniform error handling and success tracking.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: How do you build a scheduled task runner with Firebase Cloud Functions?
Create a scheduled Pub/Sub function that runs every 60 seconds and checks Firestore for jobs that are ready to execute. Each task document identifies a backend worker, its scheduled time, its status, and any serialized options the worker needs.
Q: What fields does each Firestore task document need?
Each document contains four fields: worker, status, performAt, and options. The worker names a backend JavaScript function, status tracks the task state, performAt specifies its execution time, and options carries serialized data.
Q: Why should performAt use a Firestore timestamp?
The performAt field tells the task runner when to execute a job. Using a Firestore timestamp keeps that scheduled time consistent across all clients.
Q: How does Firebase Pub/Sub scheduling work?
A scheduled function uses functions.pubsub.schedule with a five-slot cron string representing minute, hour, day, month, and weekday. Calling onRun provides a callback where the scheduled background code executes.
Q: How can users schedule tasks dynamically instead of relying on a fixed cron schedule?
Store each user-defined job as a document in a Firestore task collection. The recurring task runner then finds jobs whose performAt timestamps indicate they are due, allowing users to schedule notifications, reminders, or other backend work at different times.
Q: How often can the task runner check for scheduled jobs?
The example uses wildcard cron values so the function ticks every one minute. The transcript identifies one minute as the maximum granularity available with a crontab schedule.
Q: How many Firebase function invocations does a one-minute task runner create?
Running the task runner every 60 seconds invokes it about 45,000 times per month. The transcript says this is within Firebase's free tier and notes that multiple jobs can run within one invocation to optimize cost.
Q: What Firebase versions are used to set up this scheduled function project?
The setup calls for Firebase Tools version 6.7 and version 2.3 of the Firebase Functions module. The project is then initialized with Firebase Functions, using TypeScript in the demonstrated implementation.
Summary & Key Takeaways
-
The video initially teased building a Robo caller but pivoted to focus on creating a dynamic task runner with Firebase Cloud Functions.
-
The task runner utilizes Firestore to schedule and execute background tasks at specific times based on user-defined requirements.
-
By integrating Pub/Sub functions and Firestore timestamps, users can schedule various tasks securely and efficiently.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from Fireship 📚






Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator