Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Functions Cron Job Not Working

I am trying to set up a scheduled function in Firebase Cloud Functions. As a simple test, I have tried to recreate the sample shown on the documentation page:

const functions = require('firebase-functions')

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

However, when I run firebase serve --only functions, I get the following error:

function ignored because the pubsub emulator does not exist or is not running.

Any idea why I get this message and how I can fix it?

like image 786
Moshe Avatar asked Dec 15 '19 17:12

Moshe


People also ask

Why does cloud deployment fail?

Cloud Functions deployment can fail if the entry point to your code, that is, the exported function name, is not specified correctly. Your source code must contain an entry point function that has been correctly specified in your deployment, either via Cloud console or Cloud SDK.


2 Answers

From the documentation on Firebase's local emulator:

The Firebase CLI includes a Cloud Functions emulator which can emulate the following function types:

  • HTTPS functions
  • Callable functions
  • Cloud Firestore functions

So the local Firebase emulators don't currently support pubsub, and the error message seems to confirm that. So for the moment, you can't run pubsub triggered Cloud Functions locally.

A feature request for adding PubSub support to the emulator was filed. You might want to read up (and possibly comment) there, as the direction taken may or may not match with your needs.

The local shell does support invoking pubsub functions. That is of course quite different, but might be useful as a workaround for the moment.

like image 165
Frank van Puffelen Avatar answered Oct 16 '22 16:10

Frank van Puffelen


For what it is worth, you need to enable the pubsub emulator in firebase. Add this to your emulators block:

{
  "emulators": {
    "pubsub": {
      "port": 8085
    },
  }
}

Even then, it only creates the definition. The emulator doesn't support running the function on a schedule.

To simulate that behavior, I define a HTTP trigger, in which I manually send a message to the topic. For a schedule topic, it is firebase-schedule-<functionName>. In your case it will be firebase-schedule-scheduledFunction.

Sample code looks like:

const pubsub = new PubSub()

export const triggerWork = functions.https.onRequest(async (request, response) => {
  await pubsub.topic('firebase-schedule-scheduledFunction').publishJSON({})
  response.send('Ok')
})

Then on the command line, I trigger the HTTP function on a schedule.

while [ 1 ];
  do wget -o /dev/null -O /dev/null http://localhost:5001/path/to/function/triggerWork;
  sleep 300;
done
like image 28
Mere Vicharr Avatar answered Oct 16 '22 15:10

Mere Vicharr