Cloud Functions For Firebase Trigger on Time

Is it possible to trigger cloud functions in Firebase based on the user's time zone?

As the documentation on writing a scheduled function shows, you can specify the timezone with:

Both Unix Crontab and AppEngine syntax are supported by Google Cloud Scheduler. For example, to use Crontab to select a specific timezone in which to run a scheduled function, do something like this:

exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
.timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
.onRun((context) => {
console.log('This will be run every day at 11:05 AM Eastern!');
return null;
});

The value for timeZone must be a time zone name from the tz database. See the Cloud Scheduler reference for more information on supported properties.

There is no way here to schedule on the time of a specific user of your app, as the Cloud Functions have no way of knowing each user's timezone at deploy time (which is when the schedule is set).

How to trigger cloud function based on date stored in cloud firestore?

The built-in type to trigger scheduled functions sets the schedule at deploy time. There is no built-in trigger type to trigger at a specific dynamic time. But you can use Cloud Scheduler to dynamically schedule a callback to a Cloud Function.

Doug Stevenson wrote a good blog post about that, so I recommend checking that out: How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).

Cloud Functions for Firebase trigger on time?

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:

Sample Image

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.

Firebase Cloud Function triggered by a Table datetime field

You have to setup a scheduler.

Eg. Every minute your function starts and queries Firestore for documents with your_date_field value in the past.

  • https://firebase.google.com/docs/functions/schedule-functions

run some code 1 hour after a document is updated with cloud function and firestore

The time(s) when scheduled Cloud Functions run must be known at the moment you deploy them. That is not the case for you, since the time depends on each individual document.

In that case you can either periodically run a scheduled Cloud Function that then checks what documents it needs to update, or you can use Cloud Tasks to create a dynamic trigger. For a detailed example of the latter, see Doug's blog post How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).

Cloud functions are getting called two times, first time with body and second time without

I've seen and more-or-less logged this; for example, recently some "minimum instances=1" functions seem to start-up and run a few times a day, but the function itself isn't invoked. I also see this at deploy time (I use some custom code that deploys multiple functions at a time).

The way "cold starts" work is they have to run the function files once to FIND and ASSIGN the "functions" within. This part used to be run silently. It would be nifty if Google either returned to NOT logging this, or differentiated it in the logs.



Related Topics



Leave a reply



Submit