node.jsfirebasegoogle-cloud-functionsgoogle-cloud-storage

How to know when a cloud storage trigger retries?


I read this document and applied the best practices into my cloud function. I observed that it actually retries when an error occurs. But when I reread the doc, I feel a bit confused because when a function retries like that, how can I know that in this trigger, the function actually retries and I want to log the retries of that function in the most detailed way possible and if the retries fail, how can I detect that fail and log it out ?

I did try like in the best practices guides me, here is my example codes of cloud storage trigger

const { onObjectFinalized, onObjectDeleted, onObjectMetadataUpdated } = require('firebase-functions/v2/storage');
const env = require('./env.dev.json');
exports.storageInsertTrigger = onObjectFinalized({
    bucket: env.BUCKET_ARRAY[1],
    memory: '2GiB',
    timeoutSeconds: 120,
    retry: true
}, async event => {
    const eventAge = Date.now() - Date.parse(event.time);
    const eventMaxAge = 10000;
    const metadata = event.data ? Object.assign({}, event.data.metadata) : {};
    const eventId = event.id;

    // Ignore events that are too old
    if (eventAge > eventMaxAge) {
        console.log(`DROPPING EVENT TRIGGERED - PATH FILE: ${event.data.name} / EVENT ID: ${eventId} with age ${eventAge} ms.`, "[EVENT MAX AGE REACHED]");
        return Promise.resolve();
    }

    // .... Processing data .... => create object newObjFS for firestore insert.
        const functionResult = await admin.firestore().collection(myCollection).doc(eventId).set(newObjFS).then(() => {
            console.log(`FIRESTORE - Object ID: ${event.data.name} - Event ID: ${eventId}`);
            return Promise.resolve();
        }).catch((error) => {
            console.log(error);
            return Promise.reject(error);
        });
        return functionResult;
    } catch (error) {
        console.error(error);
        return Promise.reject(error);
    }
});

Solution

  • how can I know that in this trigger, the function actually retries

    Use the provided Event.context to find the eventId, which is not change between retries. It's up to you to track that eventId (persisted to a database or some other data store) on your own between invocations - Cloud Functions is not going to do that for you.

    and if the retries fail, how can I detect that fail and log it out

    There is no callback or notifications for execution failures. It's up to you to capture and handle any error to your satisfaction during the execution of the function. If you don't handle errors yourself, the error will eventually propagate back to the Cloud Functions infrastructure, which arranges for further retries until there is a successful invocation.

    Will the retry stop at the second try if the function executes successfully ?

    A function that completes successfully will not necessarily immediately stop a retries, as there is no guarantee that Cloud Functions will get the success result from your function. Function results are reported asynchronous and might get lost in the event of a system error. The retries will stop eventually if the function does not generate an error. Your code needs to have logic to insulate itself against unexpected retries.

    I suggest reading the documentation thoroughly for more specific cases and examples.