简体   繁体   中英

How to invoke firebase Schedule functions locally using pubsub emulator

I am working on cloud functions especially schedule functions. I need to trigger a function periodically each 5 minutes, but in only test step. I need to run it on pubsub emulator without deploying it.

How to do it?

I tried to use firebase shell, but it triggered only once

 exports.scheduledFunctionPlainEnglish =functions.pubsub.schedule('every 2 minutes')
 .onRun((context) => {
    functions.logger.log("this runs every 2 minutes")
    return null;
}) 

Scheduled functions are loaded to the Cloud Functions emulator runtime and are bound to the PubSub emulator topic.

But as @samstern said ( https://github.com/firebase/firebase-tools/issues/2034 ):

you'd have to manually trigger them using a Pub/Sub message.

You can do it like this:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { PubSub } from '@google-cloud/pubsub';

if (!admin.apps.length) {
  admin.initializeApp();
}

const pubsub = new PubSub({
  apiEndpoint: 'localhost:8085' // Change it to your PubSub emulator address and port
});

setInterval(() => {
  const SCHEDULED_FUNCTION_TOPIC = 'firebase-schedule-yourFunctionName';
  console.log(`Trigger sheduled function via PubSub topic: ${SCHEDULED_FUNCTION_TOPIC}`);
  const msg = await pubsub.topic(SCHEDULED_FUNCTION_TOPIC).publishJSON({
    foo: 'bar',
  }, { attr1: 'value1' });
}, 5 * 60 * 1000); // every 5 minutes

Additional info about this concept (thanks to @kthaas):

  1. https://github.com/firebase/firebase-tools/pull/2011/files#diff-6b2a373d8dc24c4074ee623d433662831cadc7c178373fb957c06bc12c44ba7b
  2. https://github.com/firebase/firebase-tools/pull/2011/files#diff-73f0f0ab73ffbf988f109e0a4c8b3b8a793f30ef33929928a892d605f0f0cc1f

As you said, you can use firebase shell to run your function once. And in firebase shell, you can use NodeJS commands.

Use setInterval

Inside firebase functions:shell , use setInterval to run your function every 2 minutes.

user@laptop:~$ firebase functions:shell

✔  functions: functions emulator started at http://localhost:5000
i  functions: Loaded functions: myScheduledFunction
firebase > setInterval(() => myScheduledFunction(), 120000)

> this runs every 2 minutes

Single line script

Since version 8.4.3 of firebase-tools, and especially this PR , the pipe solution does not work anymore.

In Bash, you can even pipe the setInterval command to firebase shell

user@laptop:~$ echo "setInterval(() => myScheduledFunction(), 120000)" | firebase functions:shell

For those of you seeing this in 2023, it's still not supported.

My solution was to abstract the code that does the "work" out of functions.pubsub.schedule and into their own functions. Then create a separate file (i added it at the top of the functions folder) with a setInterval inside it that fires the aforementioned abstracted function.

For example, somewhere in your code:

exports.myScheduledFunctionCode = () => {
  console.log('why, hello there interval');
}

And in the timers.js (for example) file at the top of the /functions directory:

setInterval(() => {
  myScheduledFunctionCode();
}, 60000);

Then, you can fire up your Firebase Emulator suite. In another Terminal session, just run a vanilla $ node functions/timers.js . Now your scheduled function code is running, and your whole emulator suite too.

Hope this helps someone!

This is currently not supported for scheduled functions. The documentation states:

Using the shell, you mock data and perform function calls to simulate interaction with products that the Emulator Suite does not currently support: Storage, PubSub , Analytics, Remote Config, Storage, Auth, and Crashlytics.

Scheduled functions are an unsupported extension of pubsub triggers.

Feel free to file a feature request with Firebase support .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM