Search code examples
node.jsfirebasegoogle-cloud-platformgoogle-cloud-functionsgoogle-cloud-storage

Firebase Cloud Functions raise time limit


I have written a cloud function for Firebase that is a https callable and is supposed to delete all files inside a folder. It looks like so:

import * as functions from "firebase-functions";
const admin = require('firebase-admin');
admin.initializeApp();
const {Storage} = require("@google-cloud/storage");
const storage = new Storage({keyFilename: "myserviceaccount.json"});
const fileBucket = "myfilebucket";


    exports.deleteFolder = functions.https.onCall(async (data, context) => {
  if (!context.auth) {
    throw new functions.https.HttpsError(
        "unauthenticated",
        "only authenticated users can add requests"
    );
  }
 
  const bucket = storage.bucket(fileBucket);
  return bucket.deleteFiles({
    prefix: `${data.folderName}`,
  }, function(err:any) {
    if (err) {
      console.log(err);
    } else {
      // eslint-disable-next-line max-len
      console.log(`All the Firebase Storage files in users/${data.folderName}/ have been deleted`);
    }
  });
});

That works fine. But my folders sometimes hold quite a lot documents, so the standard time limit of 60s that I get when deploying this function is sometimes not enough to delete all files inside the folder. So I would like to raise the time limit for computation to 540s witch is according to Cloud Functions the maximum computation time I can get. But I cannot find a possibility to do so.

How do I raise the time limit for a single https callable function?


Solution

  • As explained in the doc, you need to do as follows:

    const runtimeOpts = {
            timeoutSeconds: 540
          }
    
    
    exports.deleteFolder = functions
                            .runWith(runtimeOpts)
                            .https
                            .onCall(async (data, context) => {...});
    

    Note that you can also configure this value via the Google Cloud Console (not the Firebase Console).

    In the Google Cloud Console , select your Firebase project, select the "Cloud Functions" vertical menu item, click on the Function name, then click on the "Edit" Button. Then click on the "VARIABLEs, .... " section title as shown below and adjust the Timeout value in the corresponding field.

    enter image description here