简体   繁体   中英

Why does running firebase emulators also execute one of my functions?

I run firebase emulators:start --only functions,firestore and get the following output:

$ firebase emulators:start  --only functions,firestore
i  emulators: Starting emulators: functions, firestore
⚠  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: database, hosting, pubsub
✔  functions: Using node@10 from host.
⚠  functions: Your GOOGLE_APPLICATION_CREDENTIALS environment variable points to /Users/user/development/project/app/serviceKey.json. Non-emulated services will access production using these credentials. Be careful!
i  firestore: Firestore Emulator logging to firestore-debug.log
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "/Users/user/development/project/app/functions" for Cloud Functions...

>  TEST TEST 123

✔  functions[test-func1]: http function initialized (http://localhost:5001/project-9999d/us-east1/test-func1).
✔  functions[test-func2]: http function initialized (http://localhost:5001/project-9999d/us-east1/test-func2).
✔  functions[test-func3]: firestore function initialized.
✔  functions[test1-func1]: http function initialized (http://localhost:5001/project-9999d/us-east1/test1-func1).
✔  functions[test1-func2]: http function initialized (http://localhost:5001/project-9999d/us-east1/test1-func2).
✔  functions[test3-func1]: http function initialized (http://localhost:5001/project-9999d/us-east1/test3-func1).
✔  functions[test3-func2]: http function initialized (http://localhost:5001/project-9999d/us-east1/test3-func2).
✔  functions[test4]: http function initialized (http://localhost:5001/project-9999d/us-east1/test4).
i  functions[test5-func1]: function ignored because the pubsub emulator does not exist or is not running.

The function is defined in functions/scraper/index.js as:

exports.scraper = url => {
  console.log('TEST TEST', url)
  return null
}

It is imported and called as such in functions/cron/index.js

const functions = require('firebase-functions')
const test = require('../scraper')

const schedule = `every 6 hours`
exports.testFunction = functions.pubsub
  .schedule(schedule)
  .onRun(test.scraper('123'))

How come I see TEST TEST 123 output from this function but not my other functions? How can I avoid this function being executed when running emulators?

I am trying to test a scheduled cron function locally but it keeps being executed when emulators are run.

By writing .onRun(test.scraper('123')) you are asking pubsub to run the returned value of test.scraper('123') . That's why the function is called during environment initialization.

If you want to run test.scraper('123') every 6 hours, you have to wrap it into a function.

exports.testFunction = functions.pubsub
  .schedule(schedule)
  .onRun(() => test.scraper('123'))

To be more clear:

exports.testFunction = functions.pubsub
  .schedule(schedule)
  .onRun(scheduledFunction)

function scheduledFunction() {
  test.scraper('123')
}

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