Firebase云函数-容器工作线程超过256 MiB的内存限制,在服务29个请求后使用了258 MiB-生成缩略图

v2g6jxz6  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(83)

我正在开发一个android应用程序,用firebase作为后端。仍处于原型阶段,单用户,没有繁重的流量。我已经部署了(到目前为止)10个云函数。到目前为止没有关于内存的调整(256MB)或其他设置。其中一个设置为generateThumbnail from samples(略有修改)。当我测试我的应用程序时,新的图像上传到bucket,缩略图在同一文件夹中创建。基本上,功能按预期工作。然而,昨天,我得到了错误前的最后一条日志语句:

Container worker exceeded memory limit of 256 MiB with 258 MiB used after servicing 29 requests total. Consider setting a larger instance class and

然后是实际误差

Function invocation was interrupted. Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging

再说一次,我目前是单用户,到目前为止,功能被触发了大约50次。显然有些东西不像预期的那样工作。
这是一个函数:

exports.generateThumbnail = functions.storage.object().onFinalize(async (object) => {
// File and directory paths.
const filePath = object.name;
const contentType = object.contentType; // This is the image MIME type
const fileDir = path.dirname(filePath);
const fileName = path.basename(filePath);
const thumbFilePath = path.normalize(path.join(fileDir, `${THUMB_PREFIX}${fileName}`));
const tempLocalFile = path.join(os.tmpdir(), filePath);
const tempLocalDir = path.dirname(tempLocalFile);
const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);


//foldername in docId from pozes-test collection
const folderName = path.basename(fileDir)
const docIdFromFolderName = path.basename(fileDir)

// Exit if this is triggered on a file that is not an image.
if (!contentType.startsWith('image/')) {
    return functions.logger.log('This is not an image.');
}

// Exit if the image is already a thumbnail.
if (fileName.startsWith(THUMB_PREFIX)) {
    return functions.logger.log('Already a Thumbnail.');
}

// Cloud Storage files.
const bucket = admin.storage().bucket(object.bucket);
const file = bucket.file(filePath);
const thumbFile = bucket.file(thumbFilePath);
const metadata = {
contentType: contentType,
// To enable Client-side caching you can set the Cache-Control headers here. Uncomment below.
'Cache-Control': 'public,max-age=3600',
};

// Create the temp directory where the storage file will be downloaded.
await mkdirp(tempLocalDir)
// Download file from bucket.
await file.download({destination: tempLocalFile});
functions.logger.log('The file has been downloaded to', tempLocalFile);
// Generate a thumbnail using ImageMagick.
await spawn('convert', [tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile], {capture: ['stdout', 'stderr']});
functions.logger.log('Thumbnail created at', tempLocalThumbFile);
// Uploading the Thumbnail.
await bucket.upload(tempLocalThumbFile, {destination: thumbFilePath, metadata: metadata});
functions.logger.log('Thumbnail uploaded to Storage at', thumbFilePath);

// Once the image has been uploaded delete the local files to free up disk space.
fs.unlinkSync(tempLocalFile);
fs.unlinkSync(tempLocalThumbFile);
// Get the Signed URLs for the thumbnail and original image.

const results = await Promise.all([
thumbFile.getSignedUrl({
  action: 'read',
  expires: '03-01-2500',
}),
file.getSignedUrl({
  action: 'read',
  expires: '03-01-2500',
}),
]);

functions.logger.log('Got Signed URLs.');


const thumbResult = results[0];
const originalResult = results[1];
const thumbFileUrl = thumbResult[0];
const fileUrl = originalResult[0];
// Add the URLs to the Database



if (fileName == "image_0") {
    await admin.firestore().collection('testCollection').doc(docIdFromFolderName).update({thumbnail: thumbFileUrl});
    
    return functions.logger.log('Thumbnail URLs saved to database.');
} else {
    return ("fileName: " + fileName + " , nothing written to firestore")
}

这是我包裹里的

"dependencies": {
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.22.0",
"googleapis": "^105.0.0",
"child-process-promise": "^2.2.1",
"mkdirp": "^1.0.3"

有人能解释一下发生这种情况的原因吗?为什么这个功能超过了256MB的内存,而流量却很小?这是工作内存吗?可能是文件没有从临时文件夹中删除吗?

gab6jxml

gab6jxml1#

我已经从您给定的代码和数据重新创建了安装程序,但我没有得到任何类型的错误,像你遇到的。我也尝试了一个图像有大尺寸超过20 MB,但我的内存消耗的功能仍然徘徊在60 MB/调用。
更具体地说,我也试图锤功能提供超过40次,但仍然没有错误弹出给我。
我认为最好在您提供generateThumbnail from samples的同一个github链接下创建一个关于此问题的github问题,这样产品背后的实际工程师将为您提供支持,或者您也可以尝试联系firebase support

相关问题