Home > Mobile >  Firebase Function stops interval after 10 min
Firebase Function stops interval after 10 min

Time:07-01

I have a timer function on firebase functions.

My code below

exports.timecontroller = functions.region('europe-west1').firestore.document("DigitalTargets/{digitalTargetID}").onCreate((snap, context) => {
    const id = snap.id
    const date = new Date(snap.data().endDate.toDate())
    var countDownDate = date.getTime();
    var myfunc = setInterval(async function () {
        var now = new Date().getTime();
        var timeleft = countDownDate - now;
        db.collection('DigitalTargets').doc(snap.id).get().then(a => {
            if (!a.data().isClaimed) {
                console.log(timeleft)
                if (timeleft < 0) {
                    db.collection("DigitalTargets").doc(id).update({ isActive: false })
                    clearInterval(myfunc);
                }
            }
            else {
                clearInterval(myfunc);
            }

        })


    }, 1000);
    return true;
})

My problem is, when i create a doc it starts the count. I can see on log screen. But after 10 min it stops working. no more logs shown. After expire time its not deactivating doc

What i need: I placing targets on map and they have end time. I just want to set active false after timer finished.

is there any limitation on firebase functions? if theres ill check every 5 minutes with scheduled function.

CodePudding user response:

From the docs:

The maximum value for timeoutSeconds is 540, or 9 minutes.

https://firebase.google.com/docs/functions/manage-functions

As mentioned, you could use a scheduled function:

exports.myCrontabFunction = functions.pubsub.schedule("every 5 minutes")
    .timeZone("Europe/Berlin")
    .onRun(async (context) => { ... } );

CodePudding user response:

Sounds like you are using an Event Driven Function that starts when you create a doc.

As per this public documentation you can see that Even Driven Functions times out after 10 mins.

The alternative could be to have that event driven function to make an http request to another function with a higer time out of 60 minutes.

  • Related