简体   繁体   中英

Error: read ECONNRESET when resizing image with Firebase Cloud Functions

I'd like to resize an image stored in Firebase Storage with Firebase Functions.

Based on this example provided by the Firebase team : https://github.com/firebase/functions-samples/blob/master/quickstarts/thumbnails/functions/index.js I tried to write a function triggered by a Database event.

Here is the most interesting part of the code :

exports.myFunction = functions.database.ref('...').onWrite(event => {

        ...

        // Create thumbnails
        createThumbnail(1, ...);
        createThumbnail(2, ...);
        createThumbnail(3, ...);

         ...

        return; // <- Is this necessary ?
});

function createThumbnail(...) {

    ...

    return bucket
        .file(originalFilepath)
        .download({
            destination: tempFilePath
        })
        .then(() => {

            console.log('OK');

            ...

            // Generate a thumbnail using ImageMagick.
            return spawn('convert', [tempFilePath, '-thumbnail', dimension + 'x' + dimension + '>', tempFilePath])
                .then(() => {

                    ....

                    // Uploading the thumbnail.
                    return bucket.upload(tempFilePath, {
                            destination: thumbnailUrl
                        })
                        .then(() => {

                              ...

                            // Save thumbnailUrl in database
                            return admin.database().ref(...).set(thumbnailUrl);
                        });
                });
        });
}

Everything looks fine to me. However the code never go to the console.log('OK'); and I get this error :

Error: read ECONNRESET
    at exports._errnoException (util.js:1026:11)
    at TCP.onread (net.js:569:26)

Does somebody know what could be the error?

Thank you

The issue is that you are returning a Promise which completes before all your asynchronous jobs have completed.

When you do this:

createThumbnail(1, ...);
createThumbnail(2, ...);
createThumbnail(3, ...);
...
return;

You are starting 3 asynchronous createThumbnail tasks but you are returning right away. Therefore the Cloud Functions instance gets shut down and your 3 createThumbnail don't have time to complete and that's when you get an ECONNRESET error.

Each of the createThumbnail returns a Promise. What you need to do is use Promise.all to return a Promise which completes when the 3 createThumbnail Promises are completed:

const listOfAsyncJobs = [];
listOfAsyncJobs.push(createThumbnail(1, ...));
listOfAsyncJobs.push(createThumbnail(2, ...));
listOfAsyncJobs.push(createThumbnail(3, ...));
...
return Promise.all(listOfAsyncJobs); // This will ensure we wait for the end of the three aync tasks above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM