javascriptnode.jsfirebasegoogle-cloud-functionsgoogle-cloud-messaging

Function execution took 60000 ms, finished with status: 'error'


Getting timeout, even though function is working as intended

Function execution took 60000 ms, finished with status: 'error'

As above, I have a Firebase cloud function which sends out a notification to a specific user using their token, the code works as intended sending the notification to the user with the correct title and message. However, when looking at the logs, an error is returning.

I haven't seen much on this, and when Googled I see a lot of posts about a temporary issue with the servers that happened a few years ago.

Code Below

require("firebase-functions/logger/compat");

const functions = require("firebase-functions");
const admin = require("firebase-admin");

// to get the ADC automatically
admin.initializeApp({credential: admin.credential.applicationDefault()});

exports.pushNotificationPulse = functions.https.onRequest((request, response) => {
  console.log("Push notification event triggered [Function last updated 28/06/23]");

  const firstName = request.query.fullName.split(" ")[0];
  const FCMtoken = request.query.FCM_Token;
  let msg = request.query.msg;

  if (msg == "") {
    msg = "DEFAULT MSG IF NOT PROVIDED";
  }

  const message = {
    notification: {
      title: "TEXT HERE",
      body: msg,
    },
    token: FCMtoken,
  };

  admin
    .messaging()
    .send(message)
    .then((response) => {
      console.log(`Successfully sent message: ${response}`);
      return {status: `Successfully sent message: ${response}`};
    })
    .catch((error) => {
      console.log(`Error sending message: ${error}`);
      return {status: `Error sending message: ${error}`};
    });
});

What The Logs Say

2023-06-28T13:43:08.151324506Z D pushNotificationPulse: Function execution started
2023-06-28T13:43:08.201451Z I pushNotificationPulse: Push notification event triggered [Function last updated 28/06/23]
2023-06-28T13:43:08.810690Z I pushNotificationPulse: Successfully sent message: projects/toy-app-80345/messages/1687959788647301
2023-06-28T13:44:08.152258848Z D pushNotificationPulse: Function execution took 60000 ms, finished with status: 'error'

So even though the main actual task is executed in just over half a second (609.239 ms) it then waits for 60 seconds (60000 ms).


Solution

  • Your code is not actually sending a response to the client. The lines you have like this are not really doing anything at all:

      return {status: `Successfully sent message: ${response}`};
    

    You must use response.send or something similar to actually send a response. It's this response that terminates the function correctly. If you don't send a response, it will time out like you are observing now. See the documentation on terminating HTTP functions for more details and examples.

    Important: Make sure that all HTTP functions terminate properly. By terminating functions correctly, you can avoid excessive charges from functions that run for too long. Terminate HTTP functions with res.redirect(), res.send(), or res.end().

    Maybe you want to do this instead:

    response.send({status: `Successfully sent message: ${response}`})