Browse Source

fix: memory usage for thumbnail generation

nc-fix/thumbnail-worker
mertmit 3 months ago
parent
commit
c081e78e09
  1. 31
      packages/nocodb/src/modules/jobs/jobs/thumbnail-generator/thumbnail-generator.processor.ts

31
packages/nocodb/src/modules/jobs/jobs/thumbnail-generator/thumbnail-generator.processor.ts

@ -16,17 +16,24 @@ export class ThumbnailGeneratorProcessor {
async job(job: Job<ThumbnailGeneratorJobData>) { async job(job: Job<ThumbnailGeneratorJobData>) {
const { attachments } = job.data; const { attachments } = job.data;
const thumbnailPromises = attachments.map(async (attachment) => { const results = [];
for (const attachment of attachments) {
const thumbnail = await this.generateThumbnail(attachment); const thumbnail = await this.generateThumbnail(attachment);
return {
if (!thumbnail) {
continue;
}
results.push({
path: attachment.path ?? attachment.url, path: attachment.path ?? attachment.url,
card_cover: thumbnail?.card_cover, card_cover: thumbnail?.card_cover,
small: thumbnail?.small, small: thumbnail?.small,
tiny: thumbnail?.tiny, tiny: thumbnail?.tiny,
};
}); });
}
return await Promise.all(thumbnailPromises); return results;
} }
private async generateThumbnail( private async generateThumbnail(
@ -47,6 +54,8 @@ export class ThumbnailGeneratorProcessor {
return; return;
} }
sharp.concurrency(1);
try { try {
const storageAdapter = await NcPluginMgrv2.storageAdapter(); const storageAdapter = await NcPluginMgrv2.storageAdapter();
@ -66,8 +75,11 @@ export class ThumbnailGeneratorProcessor {
tiny: path.join('nc', 'thumbnails', relativePath, 'tiny.jpg'), tiny: path.join('nc', 'thumbnails', relativePath, 'tiny.jpg'),
}; };
await Promise.all( const sharpImage = await sharp(file, {
Object.entries(thumbnailPaths).map(async ([size, thumbnailPath]) => { limitInputPixels: false,
});
for (const [size, thumbnailPath] of Object.entries(thumbnailPaths)) {
let height; let height;
switch (size) { switch (size) {
case 'card_cover': case 'card_cover':
@ -84,9 +96,7 @@ export class ThumbnailGeneratorProcessor {
break; break;
} }
const resizedImage = await sharp(file, { const resizedImage = await sharpImage
limitInputPixels: false,
})
.resize(undefined, height, { .resize(undefined, height, {
fit: sharp.fit.cover, fit: sharp.fit.cover,
kernel: 'lanczos3', kernel: 'lanczos3',
@ -100,8 +110,7 @@ export class ThumbnailGeneratorProcessor {
mimetype: 'image/jpeg', mimetype: 'image/jpeg',
}, },
); );
}), }
);
return thumbnailPaths; return thumbnailPaths;
} catch (error) { } catch (error) {

Loading…
Cancel
Save