Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}) 
like image 957
Extinct Light Avatar asked Jul 06 '20 15:07

Extinct Light


People also ask

How to run scheduled functions in PubSub emulator?

The PubSub emulator still not yet support scheduled functions. But you can use firebase functions:shell and setInterval to simulate scheduler. NOTE: Please ensure you are running the firebase emulator locally, or the shell may call the functions in Production !! Don't exit, then it will run your functions every 60 seconds.

How do I schedule a firebase 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)) .

Is it possible to simulate scheduled functions in the firebase emulator?

The Firebase local emulator currently doesn't simulate the actual scheduled functions. The documentation says: I suggest filing a feature request with Firebase support. When you deploy a scheduled function, you are actually using Google Cloud Scheduler behind the scenes. The details are managed for you. As stated in the documentation:

Is there a firebase PubSub emulator?

Actually there is a Firebase PubSub emulator. To enable it you need to have the recent CLI installed (it's in 8.2.0 for sure) Create a test script locally to submit PubSub messages into the queue:


2 Answers

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
like image 146
Viacheslav Dobromyslov Avatar answered Nov 05 '22 12:11

Viacheslav Dobromyslov


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
like image 25
seza443 Avatar answered Nov 05 '22 13:11

seza443