首页 > 解决方案 > 使用 Node.js 在 App Engine 上长时间运行的进程

问题描述

我有一个可能需要一个多小时才能运行的 Node.js 网络爬虫。尝试在 App Engine 标准环境中运行时超时。部署它的最佳方式是什么?

此外,它触发了每天运行一次的 cron.yaml,它命中 Express 路由。有一个更好的方法吗?

这是代码的简化片段。我可以在本地运行它,并将其部署到 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.jsexpressgoogle-app-engineweb-scrapingcron

解决方案


我怀疑由于请求截止日期可能会出现问题。对于 App Engine 标准,默认设置为 60 秒。但是,如果您使用手动扩展,请求可以在标准环境中运行长达 24 小时。


推荐阅读