I'm using firebase for a project and am working on creating a schedule function using the following code. I want to log a message every minute this runs.
export const timedQuery = functions.pubsub.schedule('1 * * * *').onRun((context) => {
console.log("I am running")
return null;
})
I have the main logic of the code working under an http function and would like to see if this works locally before deploying to production. Going through firebase docs I've downloaded all of the firebase emulators and use "firebase emulators:start" to get them running. From the logs it looks like my pubsub emulator starts successfully at localhost:8085 and pubsub function is initialized however even after waiting for 2 - 3 minutes nothing prints out. Is it possible to test scheduled functions locally?
Also I created this without using google cloud scheduler since I'm only on firebase.
The Firebase local emulator currently doesn't simulate the actual scheduled functions. The documentation says:
The Firebase CLI includes a Cloud Functions emulator which can emulate the following function types:
- HTTPS functions
- Callable functions
- Cloud Firestore functions
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:
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.
I suggest refactoring your function's code into a method that you can test by invoking it directly using a test framework of your choice. You could also temporarily wrap it in an HTTP function and invoke it that way.