Reputation: 157
Suppose I have a small meteor app that shows cities and the their temperatures. These cities are saved in a DB.
I would like a server non blocking process that would periodically pull a web page and parse the temperatures let's say every hour for example, to update the values in the DB.
I'm assuming that as soon as the new values get updated, all the connected users would see the updated temperatures without even refreshing the page.
How do I achieve this as a Meteor beginner please. This process should be independent of the users, it should run every hour whether there has been users on the page or not.
Thank you!
Upvotes: 0
Views: 965
Reputation: 252
You can also use my new Steve Jobs package. It's very friendly towards the Meteor way of doing things.
Upvotes: 1
Reputation: 15442
I'd personally use meteor-job-collection to do this. I've just pulled some code from my project which will give you a good starting point. Just include this file in your server code and add the parsing code to the job:
const parse_RATE = 1000 * 60 * 60;
const parseQueue = JobCollection('parseQueue');
parseQueue.processJobs('parse', { pollInterval: parse_RATE }, function(job, cb) {
// Parse your web page
<add your code here>
// Signal that the job's done
job.done();
// Since .done() will create a new repeating job, we can now delete
// this old job so that we don't fill up the database with dead ones.
job.remove();
cb();
});
// Remove any old jobs that were set up when the server was last running
const oldJobs = parseQueue.find();
oldJobs.forEach((job) => {
parseQueue.cancelJobs([job._id]);
parseQueue.removeJobs([job._id]);
});
// Create a new parse job
//
// We parse at 4am every day when the server is the least busy.
parseQueue.startJobServer();
const job = new Job(parseQueue, 'parse', {});
job.priority('normal')
.repeat({ schedule: parseQueue.later.parse.recur().every(1).hour() })
.save();
Upvotes: 1
Reputation: 20256
Use the percolate:synced-cron package to create a cron job. Runs on the server. It can be scheduled to run with any periodicity using momentjs.
Upvotes: 3