<commit_before>import { getSetting, registerSetting, debug } from 'meteor/vulcan:core';
import { /*updateScore,*/ batchUpdateScore } 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'));

  if (scoreInterval > 0) {

    VoteableCollections.forEach(collection => {

      // active items get updated every N seconds
      Meteor.setInterval(async function () {

        // let updatedDocuments = 0;

        // console.log('tick ('+scoreInterval+')');
        // collection.find({'inactive': {$ne : true}}).forEach(document => {
        //   updatedDocuments += updateScore({collection, item: document});
        // });

        const updatedDocuments = await batchUpdateScore(collection, false, false);

        debug(`[vulcan:voting] Updated scores for ${updatedDocuments} active documents in collection ${collection.options.collectionName}`)

      }, scoreInterval * 1000);

      // inactive items get updated every hour
      Meteor.setInterval(async function () {


        // let updatedDocuments = 0;
        //
        // collection.find({'inactive': true}).forEach(document => {
        //   updatedDocuments += updateScore({collection, item: document});
        // });

        const updatedDocuments = await batchUpdateScore(collection, true, false);

        debug(`[vulcan:voting] Updated scores for ${updatedDocuments} inactive documents in collection ${collection.options.collectionName}`)

      }, 3600 * 24 * 1000);

    });
  }
});

<commit_msg>Refactor score update cron job<commit_after>import { getSetting, registerSetting, debug } from 'meteor/vulcan:core';
import { /*updateScore,*/ batchUpdateScore } from './scoring.js';
import { VoteableCollections } from '../modules/make_voteable.js';
import { SyncedCron } from 'meteor/percolatestudio:synced-cron';

registerSetting('voting.scoreUpdateInterval', 60, 'How often to update scores, in seconds');
const scoreInterval = parseInt(getSetting('voting.scoreUpdateInterval', 60));

SyncedCron.options = {
  log: true,
  collectionName: 'cronHistory',
  utc: false,
  collectionTTL: 172800
};

Meteor.startup(function () {
  SyncedCron.add({
    name: 'updateScoreActiveDocuments',
    schedule(parser) {
      return parser.text(`every ${scoreInterval} seconds`);
    },
    job() {
      VoteableCollections.forEach(collection => {
        batchUpdateScore(collection, false, false);
      });
    }
  });
  SyncedCron.add({
    name: 'updateScoreInactiveDocuments',
    schedule(parser) {
      return parser.text('every 24 hours');
    },
    job() {
      VoteableCollections.forEach(collection => {
        batchUpdateScore(collection, true, false);
      });
    }
  });
});

{'fraction_non_alphanumeric': 0.1365338781069118, 'fraction_numerical': 0.010554988083077971, 'mean_word_length': 3.5550387596899227, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3094019', 'n_tokens_mistral': 868, 'n_tokens_neox': 803, 'n_words': 188}