Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an workaround to handle error “Error: 4 DEADLINE_EXCEEDED: Deadline exceeded” in Cloud Functions

I am writing to Firestore using batch writes of Cloud Functions. At that time, Error: 4 DEADLINE_EXCEEDED: Deadline exceeded is displayed and writing is interrupted.

After looking at this question,Error: 4 DEADLINE_EXCEEDED: Deadline Exceeded at Object.exports.createStatusError - GCP I knew that the content of the error reached the quota for writing, but I don't know how to deal with the quota. If you know how to solve it, please let me know.

Here is my code.

exports.batchTest = functions.region('asia-northeast1').pubsub.schedule('every 60 minutes').onRun(async (snapshot, context) => {


    let db = admin.firestore();
    // Get a new write batch
    let batch = db.batch();

    const userRef = db.collection('user');
    const userQs = await userRef.get();

    userQs.forEach(userDocs => {
        let userId = userDocs.id;
        let likeRef = db.collection('like').doc(userId).collection('like').get().then(docs => {

            if (docs.exists) {
                const subLikeRef = db.collection('like').doc(userId).collection('like').doc(docs.id);
                batch.delete(subLikeRef)
                    .then(function () {
                        console.log("success");
                        return true;
                    })
                    .catch(function (error) {
                        console.error("Error", error);
                        return true;
                    });

            }
            return null;
        });


    });




    // Commit the batch
    return batch.commit().then(function () {
        // ...
        return null;
    });


}
);
like image 387
Masahiro Aoki Avatar asked Dec 03 '25 00:12

Masahiro Aoki


1 Answers

this error is related to the requests that you're making are taking too long.

In order to avoid this, you can either modify your logic to write a smaller batch of data each time your function is triggered or you can try to increase the timeout value.

You can take a look at this firebase documentation and this document to increase the timeout value.

like image 100
Chris32 Avatar answered Dec 04 '25 14:12

Chris32



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!