Vulcan/packages/vulcan-voting/lib/server/cron.js

44 lines
1.4 KiB
JavaScript
Raw Normal View History

import { getSetting, registerSetting } from 'meteor/vulcan:core';
import { updateScore } from './scoring.js';
import { VoteableCollections } from '../modules/make_voteable.js';
registerSetting('voting.scoreUpdateInterval', 60, 'How often to update scores, in seconds');
// TODO use a node cron or at least synced-cron
Meteor.startup(function () {
const scoreInterval = parseInt(getSetting('voting.scoreUpdateInterval', 60));
if (scoreInterval > 0) {
VoteableCollections.forEach(collection => {
// active items get updated every N seconds
Meteor.setInterval(function () {
let updatedDocuments = 0;
// console.log('tick ('+scoreInterval+')');
collection.find({'inactive': {$ne : true}}).forEach(document => {
updatedDocuments += updateScore({collection, item: document});
});
2017-09-13 10:22:21 +02:00
// console.log(`Updated ${updatedDocuments} active documents in collection ${collection.options.collectionName}`)
}, scoreInterval * 1000);
// inactive items get updated every hour
Meteor.setInterval(function () {
let updatedDocuments = 0;
collection.find({'inactive': true}).forEach(document => {
updatedDocuments += updateScore({collection, item: document});
});
2017-09-13 10:22:21 +02:00
// console.log(`Updated ${updatedDocuments} inactive documents in collection ${collection.options.collectionName}`)
}, 3600 * 1000);
});
}
});
2016-12-09 09:11:20 +01:00