0

My index.ts has:

exports.foo = functions.https.onCall(async (data, context) => {
  console.log('Hello World');
  return null;
});

To deploy, I run:

firebase deploy --only functions:foo

To test, I do:

final callable = FirebaseFunctions.instance.httpsCallable('foo');
await callable.call();

First time when the function execution started, my function body runs, but the second time (don't know how it gets invoked), my function body doesn't run. Is this a standard behavior, am I also getting charged for the automatic second invocation?

enter image description here

NOTE: I've read several posts like this, this, this, this etc before asking this question but none of them seemed to work for me.

iDecode
  • 22,623
  • 19
  • 99
  • 186
  • How do you invoke your function? From your browser with JS code? And which HTTP verb do you use? Post? Put? Delete? – guillaume blaquiere Apr 21 '22 at 10:10
  • @guillaumeblaquiere I'm invoking it on press of a button in my Flutter app. I think I don't need to use `Post`/`Put`/`Delete` as this is a callable function. Correct me if I'm wrong, thanks – iDecode Apr 21 '22 at 15:12
  • I can confirm that I see the same type of behavior the last days. There seems to be some known issue that both produce response errors in the logs and also seems to invoke functions without running its code a second time. Have a look at Priyashree's comments on https://stackoverflow.com/questions/71869587/gcp-logs-show-function-function-execution-took-xxx-ms-finished-with-status-re – Robert Sandberg Apr 22 '22 at 21:12

3 Answers3

0

I don't know flutter, but you run it "as if you were in a browser". In addition, pressing a button usually submit something (I mean, it's not a GET request, most of the time).

So, the combination of both PLUS your issue lead me to think to a preflight request. Check the HTTP verb before performing the processing.

guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
  • I'm not very familiar with HTTP thing but I noticed that the function only runs twice if starts from a "cold start", otherwise it just gets called once. Can you draw some conclusion now? – iDecode Apr 21 '22 at 19:14
0

I've seen and more-or-less logged this; for example, recently some "minimum instances=1" functions seem to start-up and run a few times a day, but the function itself isn't invoked. I also see this at deploy time (I use some custom code that deploys multiple functions at a time).

The way "cold starts" work is they have to run the function files once to FIND and ASSIGN the "functions" within. This part used to be run silently. It would be nifty if Google either returned to NOT logging this, or differentiated it in the logs.

LeadDreamer
  • 3,303
  • 2
  • 15
  • 18
  • *"I also see this at deploy time"* -- Correct. I'm seeing that too but thought it was by design. Could you please point me to a reference on *"The way cold starts work is they have to run the function files once to FIND and ASSIGN the functions within"*. This way I can accept your answer. Thanks – iDecode Apr 24 '22 at 06:54
  • It's not publically noted, although I'll check - but think about it: the "functions" themselves are "just" exports - the file has to be run to CREATE that export. The firebase CLI runs the file(s), and attaches the exported functions to the various indicated triggers. After all, you can have more than one function in a file (I have a file that scans a directory structure and exports all the functions in one go...) cf. https://firebase.google.com/docs/functions *b. Cloud Build retrieves the function code and builds the function source.* – LeadDreamer Apr 24 '22 at 18:59
0

My error (package.json):

"scripts": {
    "local-mac": "npm run build && NODE_ENV=development firebase emulators:start --inspect-functions 9999 --only auth,hosting,functions,firestore,pubsub",
  }

Delete: --inspect-functions 9999

Works for me.

eglease
  • 2,445
  • 11
  • 18
  • 28