如何修复不会停止执行的Google Cloud函数中的Node js函数?

时间:2018-08-10 23:33:59

标签: javascript node.js google-cloud-functions

我下面在Google Cloud函数中有一个nodejs函数,该函数不断返回ESOCKETTIMEDOUT错误,并且连续执行而没有停止。

   exports.generateTHUMBPIC = functions.storage.object('productpics').onFinalize((object, context) => {

// [START eventAttributes]
const fileBucket = object.bucket; // The Storage bucket that contains the file.
const filePath = object.name; // File path in the bucket.
const contentType = object.contentType; // File content type.
const resourceState = object.resourceState; // The resourceState is 'exists' or 'not_exists' (for file/folder deletions).
const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.

const fileDir1 = path.dirname(filePath);
const fileName0 = path.basename(filePath);

const fileDir = fileDir1.replace('productpics/', '')

var listingid = fileDir.split("*")[1];
var userid = fileDir.split("*")[0];
var productid = fileDir.split("*")[2];
var thumbname = fileDir.split("*")[3]; // thumbnail 1 or 2 or 3
thumbarray.push(thumbname);



const thumbFilePath = path.normalize(path.join(fileDir1, `${THUMB_PREFIX}${productid}`));
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);

// [END eventAttributes]

// [START stopConditions]
// Exit if this is triggered on a file that is not an image.
if (!contentType.startsWith('image/')) {
    console.log('This is not an image.');
    return null;
}


// Exit if the image is already a thumbnail.
if (productid.startsWith(THUMB_PREFIX)) {
    console.log('Already a Thumbnail.');
    return null;
}

// Download file from bucket.
const bucket = gcs.bucket(fileBucket);
const file = bucket.file(filePath);
const thumbFile = bucket.file(thumbFilePath);

const metadata = {
    contentType: contentType,
};

return mkdirp(tempLocalDir).then(() => {
    // Download file from bucket.
    return file.download({
        destination: tempLocalFile
    });
}).then(() => {

    return spawn('convert', [tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile], {
        capture: ['stdout', 'stderr']
    });
}).then(() => {
    // console.log('Thumbnail created at', tempLocalThumbFile);
    // Uploading the Thumbnail.
    return bucket.upload(tempLocalThumbFile, {
        destination: thumbFilePath,
        metadata: metadata
    });
}).then(() => {

    fs.unlinkSync(tempLocalFile);
    fs.unlinkSync(tempLocalThumbFile);
    const config = {
        action: 'read',
        expires: '03-01-2500',
    };
    return Promise.all([
        thumbFile.getSignedUrl(config),
        file.getSignedUrl(config),
    ]);
}).then((results) => {

    const thumbResult = results[0];
    const originalResult = results[1];
    const thumbFileUrl = thumbResult[0];
    const fileUrl = originalResult[0];

    var updates = {};


    updates['Listings/' + listingid + "/" + thumbname + "thumb"] = thumbFileUrl;

    return admin.database().ref().update(updates);

       }).then(() => console.log('Thumbnail URLs saved to database.'));
     });

这是完整的错误

  
    

ESOCKETTIMEDOUT             在ClientRequest。 (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:816:19)             在ClientRequest.g(events.js:292:16)             在emitNone(events.js:86:13)             在ClientRequest.emit(events.js:185:7)             在TLSSocket.emitTimeout(_http_client.js:630:10)             在TLSSocket.g(events.js:292:16)             在emitNone(events.js:86:13)             在TLSSocket.emit(events.js:185:7)             在TLSSocket.Socket._onTimeout(net.js:348:8)             在ontimeout(timers.js:386:11)

  

是什么原因导致此错误并反复执行该函数而没有停止,我该如何解决?

0 个答案:

没有答案