Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Functions for Firebase trigger on time?

I am looking for a way to schedule Cloud Functions for Firebase or in other words trigger them on a specific time.

like image 428
ahsan Avatar asked Mar 14 '17 15:03

ahsan


People also ask

When should I use Firebase cloud function?

You should use Cloud Functions for Firebase if you're a developer building a mobile app or mobile web app. Firebase gives mobile developers access to a complete range of fully managed mobile-centric services including analytics, authentication and Realtime Database.


2 Answers

Update 2019-04-18

There is now a very simple way to deploy scheduled code on Cloud Functions through Firebase.

You can either use a simple text syntax:

export scheduledFunctionPlainEnglish = functions.pubsub.schedule('every 5 minutes').onRun((context) => {     console.log('This will be run every 5 minutes!'); }) 

Or the more flexible cron table format:

export scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *').onRun((context) => {     console.log('This will be run every day at 11:05 AM UTC!'); }); 

To learn more about this, see:

  • The Scheduling Cloud Functions for Firebase blog post introducing the feature.
  • The documentation on scheduled functions.

Note that your project needs to be on a Blaze plan for this to work, so I'm leaving the alternative options below for reference.

If you want to schedule a single invocation of a Cloud Function on a delay from within the execution of another trigger, you can use Cloud Tasks to set that up. Read this article for an extended example of how that can work.

Original answer below...


There is no built-in runat/cron type trigger yet.

For the moment, the best option is to use an external service to trigger a HTTP function periodically. See this sample in the functions-samples repo for more information. Or use the recently introduced Google Cloud Scheduler to trigger Cloud Functions through PubSub or HTTPS:

enter image description here

I also highly recommend reading this post on the Firebase blog: How to Schedule (Cron) Jobs with Cloud Functions for Firebase and this video: Timing Cloud Functions for Firebase using an HTTP Trigger and Cron.

That last link uses cron-job.org to trigger Cloud Functions, and works for projects that are on a free plan. Note that this allows anyone to call your function without authorization, so you may want to include some abuse protection mechanism in the code itself.

like image 74
Frank van Puffelen Avatar answered Sep 22 '22 13:09

Frank van Puffelen


What you can do, is spin up an AppEngine instance that is triggered by cron job and emits to PubSub. I wrote a blog post specifically on that, you might want to take a look:

https://mhaligowski.github.io/blog/2017/05/25/scheduled-cloud-function-execution.html

like image 21
mhaligowski Avatar answered Sep 22 '22 13:09

mhaligowski