I have the following script to import raw images from an api:
let importImages = function (meal,accessToken) {
let imagesData = meal.afbeeldingenInfo;
let mealFolder = 'client/import/meal-'+meal.maaltijdId;
ensureExists(mealFolder, 0o755, function(err) {
if (err) {
winston.error('Not able to create a folder : %s', err.message);
} else {
winston.info('Folder %s succesfully created', mealFolder);
for (let index in imagesData) {
if (imagesData.hasOwnProperty(index)) {
let imageData = imagesData[index];
axios({
method: 'get',
responseType: 'stream',
url: SYS_URL + '/MaaltijdAfbeelding/' + imageData.id,
headers: {'Authorization': 'Bearer ' + accessToken},
}).then(response => {
winston.info('Got successful response from the API');
// let image = response.data;
let filePath = mealFolder + '/'+ imageData.name;
response.data.pipe(fs.createWriteStream(filePath));
winston.info('Image %s succesfully imported', imageData.name);
response.data.on('end', () => {
// generate images for all sizes
for (var key in imageSizes) {
if (imageSizes.hasOwnProperty(key)) {
let size = imageSizes[key];
gm(filePath).resize(size).strip().interlace('Line')
.write(mealFolder + '/' + key + '-' + imageData.name, function (err) {
if (!err) {
winston.info('Image %s successfully minimized to '+ size +'px', imageData.name)
} else {
winston.info('Not able to minimize image %s due to error: %s', imageData.name, err.message)
}
});
}
}
})
}).catch(error => {
winston.error('Not able to import image %s due to error: %s', imageData.name, error.message);
});
}
}
}
});
};
However when I execute this, after a few seconds the node script freezes the system entirely.
Should this not be done in this manner? If so what is a better practice for resizing images?
Sounds like you are running too many processes at once. I would recommend running these processes through a queue.
This will mean only one process runs at once but but if any tasks fail, you will be able to reprocess them in the queue. This is a great library for running queues