Search code examples
firebasegoogle-cloud-functionsfirebase-tools

Do Firebase Scheduled Functions run automatically on the emulator?


I am trying to build a scheduled function on Firebase using the emulator. I've set things up as shown and have verified that PubSub is running on my emulator. However, nothing happens.

I have the following function:

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

I can verify the function works using firebase functions:shell:

firebase functions:shell
i  functions: Loaded functions: createUserDocument, newSlackUserNotify, scheduledFunction
⚠  functions: The following emulators are not running, calls to these services will affect production: firestore, database, pubsub, storage, eventarc
firebase > scheduledFunction()
'Successfully invoked function.'
firebase > >  This will be run every 5 minutes!

My firebase.json:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "emulators": {
    "auth": {
      "port": 9099,
      "host": "0.0.0.0"
    },
    "firestore": {
      "port": 8080,
      "host": "0.0.0.0"
    },
    "functions": {
      "port": 5001,
      "host": "0.0.0.0"
    },
    "pubsub": {
      "port": 8085,
      "host": "0.0.0.0"
    },
    "ui": {
      "enabled": true
    },
    "singleProjectMode": true
  },
  "functions": [
    {
      "source": "apps/functions",
      "codebase": "default",
      "ignore": ["node_modules", ".git", "firebase-debug.log", "firebase-debug.*.log"],
    }
  ]
}

I can see here that someone suggests using a trigger, but shouldn't scheduled functions "just work"?

I have also verified that the scheduled function works when deployed, so this seems to be an emulator-specific thing.

The only possible thing is that SLF4J seems to have not loaded, per pubsub-debug.log:

This is the Google Pub/Sub fake.
Implementation may be incomplete or differ from the real system.
Jan 13, 2023 2:09:25 PM com.google.cloud.pubsub.testing.v1.Main main
INFO: IAM integration is disabled. IAM policy methods and ACL checks are not supported
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Jan 13, 2023 2:09:26 PM com.google.cloud.pubsub.testing.v1.Main main
INFO: Server started, listening on 8085
Jan 13, 2023 2:09:31 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected HTTP/2 connection.

Solution

  • As Chris mentioned in above comment.Emulator doesn't support scheduled functions yet. There is a feature request raised for Support for scheduled functions in emulator in github. You can also add your concern over there. You need to trigger them manually using the firebase functions:shell command .You can run the scheduled functions on a interval:

    firebase > setInterval(() => yourScheduledFunc(), 60000)
    //This will run your functions every 60 seconds.
    

    You can also check this stackoverflow thread1 & thread2