[BUG] in sample:
grega913 opened this issue · 0 comments
Which sample has a bug?
generate-thumbnail
Sample name or URL where you found the bug
https://github.com/firebase/functions-samples/blob/main/generate-thumbnail/functions/index.js
How to reproduce the issue
Normal use of function, single user, function executed 30 times
Failing Function code used (if you modified the sample)
`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")
}`
Steps to set up and reproduce
Debug output
Container worker exceeded memory limit of 256 MiB with 258 MiB used after servicing 29 requests total. Consider setting a larger instance class and
Errors in the
console logs
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
Screenshots
Expected behavior
Function to use less than 256MB memory
Actual behavior
Provided error
This only happened once. Witout any changes, normal memor usage is around 100 MB . .