MVT KVM
MVT KVM

Reputation: 198

Handle Deadline exceeded Firebase Cloud Functions

In using firebase functions to insert data in my firebase firestore. They take data from differents APIs. I write and read a lot of documents - probably why I get this error. I've read a lot of Stackoverflow posts and issues on Github too and I found that this error could have many reasons. I'm 99% sure that this as nothing to do with Promises. I'm using async / await

How can I handle it, what are the solutions?

The exact errors when I catch it : Error: 4 DEADLINE_EXCEEDED: Deadline exceeded Firebase Cloud Functions

Here is the code l1b3rty asked :


if (plans.results.length != 0) {
    const batch = db.batch()
    plans.results.forEach(async (plan) => {
        const jiraKey = plan.planItem.self.split('/').pop()
        let wyzioProjectId
        const projects = await db.collection('projects').where('jiraKey', '==', jiraKey).get()
        if (!projects.empty) {
            projects.forEach(async (project) => {
            wyzioProjectId = project.data().wyzioId
            const jiraUserId = plan.assignee.self.split('=').pop()
            const username = await user.findOne('user?accountId=' + jiraUserId)
            if (username != null) {
                let wyzioUserId
                const users = await db.collection('users').where('name', '==', username).get()
                if (!users.empty) {
                    users.forEach(user => {
                        wyzioUserId = user.data().id
                    })
                }
       batch.set(db.collection('plans').doc(JSON.stringify(plan.id)), {
                        'TempoId': plan.id,
                        'jiraKey': jiraKey,
                        'projectId': wyzioProjectId,
                        'userId': wyzioUserId,
                        'username': username,
                        'startDate': plan.startDate,
                        'values': plan.dates.values,
                        'status': 1,
                        'syncStatus': 1
                    }, { merge: true })
                }
            })
         } else {
            let key = "tempo_plan_" + plan.jiraWorkLogId + "_no_ref_project"
            let message = "Impossible d'ajouter le plan : " + plan.jiraWorkLogId + ", il n'appartient à aucun projet de la base de données"
            await notifications.addNotifications(db, key, message)
        }
    })
    try {
        await batch.commit()
        await incrementOffsetTempo(db, 'tempo_plans')
        console.log("--- tempo_plans_done ---")
    } catch {
        let key = "tempo_plan_" + plan.jiraWorkLogId + "_error_sync"
        let message = "Impossible d'ajouter le plan : " + plan.jiraWorkLogId + ", veuillez contrôler ses informations"
        await notifications.addNotifications(db, key, message)
    }
}

The batch.set() is not correctly aligned

Upvotes: 0

Views: 506

Answers (1)

MVT KVM
MVT KVM

Reputation: 198

I solved my problem replacing the foreachloop with a for of loop. Here is an example:

if (plans.results.length != 0) {
    const batch = db.batch()
    for (const plan of plans.results) {
        const jiraKey = plan.planItem.self.split('/').pop()
        const projects = await db.collection('projects').where('jiraKey', '==', jiraKey).get()
        // ... 
        //set the batch with info
    }
    try {
        await batch.commit()
        await incrementOffsetTempo(db, 'tempo_plans')
        console.log("--- tempo_plans_done ---")
    } catch (error) {
        let key = "tempo_plans_error_sync"
        let message = "Impossible d'ajouter les plans. Erreur : " + error
        await notifications.addNotifications(db, key, message)
    }
}

Upvotes: 1

Related Questions