【问题标题】:Long running processes on App Engine with Node.js使用 Node.js 在 App Engine 上长时间运行的进程
【发布时间】: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


    【解决方案1】:

    我怀疑问题可能是由于请求截止日期而发生的。对于 App Engine 标准,默认设置为 60 秒。但是,如果您使用manual scaling,则请求在标准环境中最多可以运行 24 小时。

    【讨论】:

    • 这是“idle_timeout”设置吗?这对基本缩放有不同的限制吗?我设置了idle_timeout: 120m 并得到了超出范围的错误。但是 60m 有效。
    • 不需要指定“idle_timeout”。请求截止日期由扩展设置决定,因此您唯一需要更改的是扩展设置,问题应该得到解决。
    猜你喜欢
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    相关资源
    最近更新 更多