Cron & Cloud Functions for Firebase

The Serverless Cron we wanted!

Triggering Firebase Cloud Functions on a regular basis with something like Cron has been a discussed at length. Suggested solutions included:

  • App Engine Tasks
  • App Engine running Cron
  • Google Compute Engine running Cron
  • public HTTPS Cloud Function & Cron-as-a-Service tools like hook.io

Sadly, these solutions involved configuring and managing servers via Google Cloud Console, paying for 24/7 uptime (regardless of how frequent you needed triggers) or exposing public endpoints into your system.

Firebase Cron for Cloud Functions logo

Image by Google Cloud Platform

More recently, GCP Cloud Scheduler was released, a fully managed enterprise-grade cron job scheduler. This finally allowed scheduled Cloud Functions when combined with PubSub topics!

Unfortunately, this still required wiring up all the pieces yourself and using gcloud or the Google (not Firebase) Cloud Console to spin up all the resources. Pascal Luther wrote a great guide for doing so Scheduling Firebase Cloud Functions with Cloud Scheduler.

But, as of 19th April 2019 Firebase added native support for writing and deploying scheduled Cloud Functions, no need to configure your own Cloud Scheduler or Cloud PubSub Topics 🎉

Firebase orchestration image

Image by Google Cloud Platform

Writing Scheduled Cloud Functions

const functions = require('firebase-functions');

exports.englishSyntax = functions.pubsub
  .schedule('every 10 minutes')
  .timeZone('Australia/NSW')
  .onRun(context => {
    console.log('triggered every 10 minutes', context);
  });

exports.cronSyntax = functions.pubsub
  .schedule('5 * * * *')
  .timeZone('Pacific/Auckland')
  .onRun(context => {
    console.log('triggered every 5 minutes', context);
  });

GitHub Gist code sample file: firebase-cron-cloud-functions-js

Cloud Scheduler support is provided by the standard firebase-functions package. The PubSub trigger type now supports schedule in addition to topic triggers. It’s here we define the cron schedule and other configuration.

On deployment, this creates:

  • a Cloud Scheduler trigger with the cron config, pushing to PubSub
  • a PubSub Topic named firebase-scheduled-function_name-region
  • a Cloud Function listening to this new topic

🌐 Time Zones

You can customize which time zone your cron schedule uses to trigger an event. The default time zone is UTC (GMT). The format used is the standard zoneinfo time zone name. 'America/New_York' as an example.

🕒 Cron Schedules

Two formats are supported to define your time intervals:

  • Unix Cron String Format (Cloud Scheduler)* * * * * where each * is a number corresponding to minhourday of monthmonthday of week. Not easy reading. If you have a hard time understanding this format, you’re not alone. Crontab Guru is like regexr but for understanding Cron schedule expressions 💯
  • App Engine’s English-like format: Keywords of everyminutehoursdays, weekday names (wedfri etc) make for easy comprehension. Some examples: every 12 hoursevery 6 hours mon,wed. Read the docs for more information and examples.

💻 User Interface

The schedule trigger and cron syntax for each function are clearly displayed in the Firebase Console!

Firebase Console UI for Cron-triggered Cloud Functions

Clear Scheduled Functions in Firebase Console!

Going to the GCP Console, we can see the frequency field includes the time zone information and the PubSub topics that each trigger pushes too.

Google Cloud Console showing the underlying Cron triggers

Cloud Scheduler in Google Cloud Console

And as we can see here, the functions are triggered as expected:

Firebase Console UI of logs showing Cron-triggered Cloud Functions

Cron-triggered Cloud Functions in the Firebase Functions Log page

🚨 delete your scheduled functions once you have completed testing! 🚨

Take a look at the more complex scheduled function example, delete inactive user accounts cron, in the firebase-samples repoistory.

💰 Pricing

  • Cloud Function — pay per invocation, compute (GB/s, GHz/s) & networking as usual. As always, a generous free tier!
  • Cloud PubSub —pay per message ingestion & delivery. 10GB/month free!
  • Cloud Scheduler — 3 free jobs per month. A job is an activity/frequency pair. You are charged $0.10 per job definition (beyond the first 3). Cloud Scheduler as a product only charges for jobs, not per execution of an activity.

Conclusion

With native support of Cloud Scheduler triggers for Cloud Functions in Firebase we can finally use a serverless cron mechanism without public HTTPS Cloud Functions or mixing GCP resource configuration with our Firebase application! 💯

Huge thanks goes to the people who tinkered and wrote guide after guide until Cloud Scheduler released, and to the Firebase team for their ongoing work making an awesome product better each week!

Sources

📚 Official Firebase Scheduled Functions Documentation
📯 Official Firebase Announcement