Node.js 为什么我的云功能在完成任务后仍继续运行?

Node.js 为什么我的云功能在完成任务后仍继续运行?,node.js,firebase,google-cloud-storage,google-cloud-functions,firebase-storage,Node.js,Firebase,Google Cloud Storage,Google Cloud Functions,Firebase Storage,因此,我尝试从已上载到firebase存储的图像制作缩略图。该函数可以正确生成缩略图。但是从函数日志中可以看出,即使缩略图生成得很完美,函数仍然会重复运行 从下面的日志中可以看到,函数从2:37到2:40反复运行。我需要从终端删除该功能以停止该功能 以下是我使用的代码: export const generateEventThumbnail = functions.storage.object().onFinalize(async (object) => { const

因此,我尝试从已上载到firebase存储的图像制作缩略图。该函数可以正确生成缩略图。但是从函数日志中可以看出,即使缩略图生成得很完美,函数仍然会重复运行

从下面的日志中可以看到,函数从2:37到2:40反复运行。我需要从终端删除该功能以停止该功能

以下是我使用的代码:

export const generateEventThumbnail = functions.storage.object().onFinalize(async (object) => {

      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 fileName = path.basename(filePath); // Get the file name.

      console.log(filePath)
      console.log(fileName)

      if (!contentType.startsWith('image/')) {
        console.log('This is not an image.');
        return null;
      }




      try {

        // [START thumbnailGeneration]
        // Download file from bucket.
        const bucket = gcs.bucket(fileBucket);
        const tempFilePath = path.join(os.tmpdir(), fileName);
        const metadata = {contentType: contentType};
        await bucket.file(filePath).download({destination: tempFilePath})
        console.log('Image downloaded locally to', tempFilePath)

        // Generate a thumbnail using ImageMagick.
        await spawn('convert', [tempFilePath, '-thumbnail', '100x100>', tempFilePath])
        console.log('Thumbnail created at', tempFilePath)

        // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
        const thumbFileName = `${fileName}`;
        const thumbFilePath = `eventThumbnail/${thumbFileName}`

        // Uploading the thumbnail.
        await bucket.upload(tempFilePath, {destination: thumbFilePath,metadata: metadata})

        // Once the thumbnail has been uploaded delete the local file to free up disk space.
        fs.unlinkSync(tempFilePath)
        // [END thumbnailGeneration]


        // then save the thumbnail path to the event data in firestore database

        return Promise.resolve(null)



      }
      catch (error) {
          console.log(error)
      }




    });

如何在成功生成缩略图后停止该功能?

您正在将缩略图写回存储器:

const thumbFilePath = `eventThumbnail/${thumbFileName}`
await bucket.upload(tempFilePath, {destination: thumbFilePath,metadata: metadata})
这会再次触发函数,但您没有正确检查是否应再次处理新缩略图

这是您现在执行的唯一提前终止检查:

if (!contentType.startsWith('image/')) {
    console.log('This is not an image.');
    return null;
}
这显然对你的案子不起作用。也许您应该检查新编写的文件的路径,而不是查看其内容类型。也许跳过写入eventThumbnail的文件对您的案例有效