导入图像时 NodeJS gm 挂起
NodeJS gm hanging when importing images
我有以下脚本可以从 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);
});
}
}
}
});
};
然而,当我执行它时,几秒钟后节点脚本完全冻结了系统。
不应该以这种方式完成吗?如果是这样,调整图像大小的更好做法是什么?
听起来你一次 运行 处理太多了。我会通过队列推荐 运行 这些进程。
这意味着一次只能运行一个进程,但如果有任何任务失败,您将能够在队列中重新处理它们。 This 是 运行 队列的绝佳库
我有以下脚本可以从 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);
});
}
}
}
});
};
然而,当我执行它时,几秒钟后节点脚本完全冻结了系统。
不应该以这种方式完成吗?如果是这样,调整图像大小的更好做法是什么?
听起来你一次 运行 处理太多了。我会通过队列推荐 运行 这些进程。
这意味着一次只能运行一个进程,但如果有任何任务失败,您将能够在队列中重新处理它们。 This 是 运行 队列的绝佳库