Перейти к консоли

Schedule functions

If you want to schedule functions to run at specified times, use functions.pubsub.schedule().onRun() This convenience method creates a Google Cloud Pub/Sub topic and uses Google Cloud Scheduler to trigger events on that topic, ensuring that your function runs on the desired schedule.

Before you begin

To use this solution in your Firebase project, you must be on the Blaze billing plan. If you're not already on Blaze, upgrade your account.

Though billing is required, you can expect the overall cost to be manageable, as each Cloud Scheduler job costs $0.10 (USD) per month, and there is an allowance of three free jobs per Google account. Use the Blaze pricing calculator to generate a cost estimate based on your projected usage.

The Google Cloud Pub/Sub and Cloud Scheduler APIs must be enabled for your project. These should already be enabled for most Firebase projects; you can verify in the Google Cloud Platform Console.

Write a scheduled function

In Cloud Functions for Firebase, scheduling logic resides in your functions code, with no special deploy-time requirements. To create a scheduled function, use functions.pubsub.schedule('your schedule').onRun((context)). For example, to run a function every five minutes with AppEngine cron.yaml syntax, do something like this:

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

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:

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

Deploy a scheduled function

When you deploy a scheduled function, the related scheduler job and pub/sub topic are created automatically. The Firebase CLI echoes the topic name, and you can view the job and topic in the GCP Console. The topic is named according to the following convention:

firebase-scheduled-function_name-region

For example:

firebase-scheduled-scheduledFunctionCrontab-us-east1.