1

I keep getting this error and not quite sure what is going on.

queueFunction
Error: Process exited with code 16
at process.<anonymous> (/layers/google.nodejs.functions- 
framework/functions-framework/node_modules/@google-cloud/functions- 
framework/build/src/invoker.js:275:22)
at process.emit (events.js:315:20)
at process.EventEmitter.emit (domain.js:483:12)
at process.exit (internal/process/per_thread.js:167:15)
at Object.sendCrashResponse (/layers/google.nodejs.functions- 
framework/functions-framework/node_modules/@google-cloud/functions- 
framework/build/src/logger.js:37:9)
at process.<anonymous> (/layers/google.nodejs.functions- 
framework/functions-framework/node_modules/@google-cloud/functions- 
framework/build/src/invoker.js:271:22)
at process.emit (events.js:315:20)
at process.EventEmitter.emit (domain.js:483:12)
at processPromiseRejections (internal/process/promises.js:209:33)
at processTicksAndRejections (internal/process/task_queues.js:98:32)

queueMatch
Error: Process exited with code 16
at process.<anonymous> (/layers/google.nodejs.functions- 
framework/functions-framework/node_modules/@google-cloud/functions- 
framework/build/src/invoker.js:275:22)
at process.emit (events.js:315:20)
at process.EventEmitter.emit (domain.js:483:12)
at process.exit (internal/process/per_thread.js:167:15)
at Object.sendCrashResponse (/layers/google.nodejs.functions- 
framework/functions-framework/node_modules/@google-cloud/functions- 
framework/build/src/logger.js:37:9)
at process.<anonymous> (/layers/google.nodejs.functions- 
framework/functions-framework/node_modules/@google-cloud/functions- 
framework/build/src/invoker.js:271:22)
at process.emit (events.js:315:20)
at process.EventEmitter.emit (domain.js:483:12)
at processPromiseRejections (internal/process/promises.js:209:33)
at processTicksAndRejections (internal/process/task_queues.js:98:32) 

And this is for function I created that seems to execute sometimes and then doesn't.

exports.queueFunction = globalFunctions.runWith(runtimeOpts).https.onRequest((request, response) => {

try {
    challengeId = '';
    console.log("Running cron challenge...")
    var timesRun = 0;
    var interval = setInterval(() => {
        timesRun += 1;
        if (timesRun === 12) {
            console.log("Stopping cron challenge interval...")
            clearInterval(interval);
        }
        console.log("Executing challenge match...")
        getChallenges();
    }, 5000);
    response.status(200).send({ success: 'success' });
} catch (error) {
    response.status(500).send({ error: 'error' });
}

});

And the queue is simply checking is there is a match queue in a collection

db = admin.firestore();
const tourRef = db.collection('ChallengeQueue');
const snapshot = await tourRef.where('challengeId', '==', challengeId).get();
const queuedata = [];
if (snapshot.empty) {
    console.log('No users online.');
    return;
}
snapshot.forEach(doc => {
    if (doc.id !== playerId) {
        queuedata.push(doc.data());
    }
});
Shina
  • 2,019
  • 2
  • 19
  • 25
  • On Stack Overflow, please don't show pictures of text and code. Copy the text into the question itself and format it so that it's easy to read, copy, and search. You can edit the question to correct this using the edit link at the bottom. – Doug Stevenson Dec 24 '20 at 05:18
  • Thanks @DougStevenson noted ... thought it would help identify the error – Shina Dec 24 '20 at 05:22
  • It would be more helpful if you simply copy the text into the question. The screenshot itself doesn't add anything - it's the text that matters. [Please read this.](https://meta.stackoverflow.com/q/285551/807126). – Doug Stevenson Dec 24 '20 at 05:31
  • [This answer](https://stackoverflow.com/a/64970415/3068190) suggests that this is caused by an `unhandledException` or `unhandledRejection` event. As there is a lack of `.catch()` or try-catch blocks in your asynchronous code, you are likely running into an unhandled rejection. Add some error handling to your code and the issue should go away or at least make itself obvious. – samthecodingman Dec 26 '20 at 04:18

1 Answers1

1

Based on the error message given, the given queueFunction has nothing to do with it as it is an error in the queueMatch function.

Please provide the globalFunctions and runtimeOpts objects. Currently I can only assume that globalFunctions is defined as import * as globalFunctions from "firebase-functions".

Regarding why your interval sometimes runs, it's because you are signalling that the function is ready to be terminated before any of the interval callbacks run (see below). Once a response is returned, the "function executor" is considered safe for shutting down - you shouldn't have anything important happen after sending a response.

Your code is functionally equivalent to:

exports.queueFunction = globalFunctions.runWith(runtimeOpts).https.onRequest((request, response) => {

  try {
      challengeId = '';
      console.log("Running cron challenge...")
      var timesRun = 0;
      var interval = 1 /* some number representing the ID of the interval */;
      response.status(200).send({ success: 'success' }); // response sent, terminate function
  } catch (error) {
      response.status(500).send({ error: 'error' }); // response sent, terminate function
  }

});

Note: In the above code block, assume "terminate function" to mean that process.exit() is called immediately. In reality, the "function executor" isn't terminated immediately, but your code should assume that it is.

samthecodingman
  • 23,122
  • 4
  • 30
  • 54
  • Thanks Sam, I have edited my question with the correct image. Its same issue on both functions `const globalFunctions = functions.region('europe-west1');` and `const runtimeOpts = { timeoutSeconds: 300, memory: '1GB' };` – Shina Dec 24 '20 at 05:28
  • @Shina Could you please try to wait for the interval to finish before returning from the function? After that, update the question stating if the error persists. As this answer noted, the issue is most likely being caused by the CF being terminated too soon. In order to achieve the intended functionality you could use a loop of 12 iterations and a [synchronous wait](https://stackoverflow.com/a/47480429). If after solving that you encounter any other issue, update your question with the current status. – Ajordat Dec 24 '20 at 12:06