Javascript 在Meteor.js中运行后台任务

Javascript 在Meteor.js中运行后台任务,javascript,meteor,Javascript,Meteor,这是我的设想: 1. Scrape some data every X minutes from example.com 2. Insert it to Mongodb database 3. Subscribe for this data in Meteor App. 因为,目前我不太擅长流星这是我要做的: 1. Write scraper script for example.com in Python or PHP. 2. Run script every X minutes with

这是我的设想:

1. Scrape some data every X minutes from example.com
2. Insert it to Mongodb database
3. Subscribe for this data in Meteor App.
因为,目前我不太擅长流星这是我要做的:

1. Write scraper script for example.com in Python or PHP.
2. Run script every X minutes with cronjob.
3. Insert it to Mongodb.

有没有可能不使用Python或PHP而完全使用Meteor?我如何处理每X分钟运行一次的任务?

有一些类似Cron的系统,比如Meteor。在这里,您可以使用类似以下示例的语法注册作业,该示例取自percolate:synced cron自述文件:

SyncedCron.add({
  name: 'Crunch some important numbers for the marketing department',
  schedule: function(parser) {
    // parser is a later.parse object
    return parser.text('every 2 hours');
  }, 
  job: function() {
    var numbersCrunched = CrushSomeNumbers();
    return numbersCrunched;
  }
});

如果你想依赖操作系统级的cron作业,你可以在Meteor.js应用程序中提供一个HTTP端点。

有一些类似cron的系统,比如Meteor。在这里,您可以使用类似以下示例的语法注册作业,该示例取自percolate:synced cron自述文件:

SyncedCron.add({
  name: 'Crunch some important numbers for the marketing department',
  schedule: function(parser) {
    // parser is a later.parse object
    return parser.text('every 2 hours');
  }, 
  job: function() {
    var numbersCrunched = CrushSomeNumbers();
    return numbersCrunched;
  }
});
如果您想依赖操作系统级的cron作业,您可以在Meteor.js应用程序中提供一个HTTP端点。

我建议,我的新软件包用于在Meteor中调度后台作业

您可以使用
寄存器
复制
删除
操作

// Register the job

Jobs.register({ 
    dataScraper: function (arg) {
        var data = getData()

        if (data) {
            this.replicate({
                in: {
                    minutes: 5
                }
            });

            this.remove(); // or, this.success(data)
        } else {
            this.reschedule({
                in: {
                    minutes: 5
                }
            })
        }
    }
})

// Schedule the job to run on Meteor startup
// `singular` ensures that there is only pending job with the same configuration

Meteor.startup(function () {
    Jobs.run("dataScraper", {
        in: {
            minutes: 5
        }, 
        singular: true
    })
})
根据您的偏好,您可以将结果存储在数据库中,作为作业历史记录的一部分,也可以将其完全删除

我可以建议,我的新软件包用于在Meteor中安排后台作业

您可以使用
寄存器
复制
删除
操作

// Register the job

Jobs.register({ 
    dataScraper: function (arg) {
        var data = getData()

        if (data) {
            this.replicate({
                in: {
                    minutes: 5
                }
            });

            this.remove(); // or, this.success(data)
        } else {
            this.reschedule({
                in: {
                    minutes: 5
                }
            })
        }
    }
})

// Schedule the job to run on Meteor startup
// `singular` ensures that there is only pending job with the same configuration

Meteor.startup(function () {
    Jobs.run("dataScraper", {
        in: {
            minutes: 5
        }, 
        singular: true
    })
})
根据您的偏好,您可以将结果存储在数据库中,作为作业历史记录的一部分,也可以将其完全删除