【发布时间】:2019-01-11 14:46:45
【问题描述】:
我有一个可能需要一个多小时才能运行的 Node.js 网络爬虫。尝试在 App Engine 标准环境中运行时超时。部署它的最佳方式是什么?
此外,它触发了每天运行一次的 cron.yaml,它命中 Express 路由。有没有更好的方法来做到这一点?
这是代码的简化 sn-p。我可以在本地运行它,并将其部署到 App Engine。 dlLinkArray 中的少量链接运行良好。但是数量更大(数千),它似乎没有做任何事情。使用报告显示它运行了几秒钟。
const Storage = require('@google-cloud/storage');
const storage = new Storage();
function startDownload(){
dlLinkArray = [/*Array of objects with URL and Filename {link: 'http://source.com', filename: 'file123456'} */]; //About 10,000 links/files
var promises = [];
dlLinkArray.forEach(record =>{ //create array of nested promises
promises.push(
uploadFile(bucketName, record.link, record.filename)
.then((x) => {
if(x[1].name) //rename file from whatever is on the remote server to a usefull ID
return renameFile(bucketName, x[1].name, record.filename + ".pdf"); //renameFile uses storage.file.move to rename, returns a promise
else
return x;
})
);
});
return Promise.all(promises);
}
function uploadFile(bucketName, fileURL, reName) {
// Uploads a remove file to the Cloud Storage bucket
return storage
.bucket(bucketName)
.upload(fileURL, {
gzip: true,
metadata: {
cacheControl: 'public, max-age=31536000',
},
});
}
/*Express Route*/
app.get('/api/whatever/download', (req, res) => {
buckets2.startDownload().then(() => console.log("DONE"));
res.status(200).send("Download Started");
});
【问题讨论】:
标签: node.js express google-app-engine web-scraping cron