<commit_before>var dbService = require('./dbservice');
var logger = require('./logger').logger;

var plannedRun;

var rescheduleCleaner = async function(){
  if(plannedRun)
    clearTimeout(plannedRun);
  delete plannedRun;

  var planNextRun = function(expires){
    expires = new Date(expires);
    var currentTime = new Date();
    var sleepMillis = expires.getTime() - currentTime.getTime();
    sleepMillis = Math.max(sleepMillis, 0);
    logger.verbose("Planning expired token cleanup for " + expires.toISOString());
    plannedRun = setTimeout(function(){
      return dbService.deleteExpiredTokens().then(function(){
        rescheduleCleaner();
      });
    }, sleepMillis);
  };

  var expires = await dbService.Token.min('expires');
  if(expires !== null)
    planNextRun(expires);
};

module.exports.rescheduleCleaner = rescheduleCleaner;<commit_msg>Fixed infinite loop when database has no tokens.<commit_after>var dbService = require('./dbservice');
var logger = require('./logger').logger;

var plannedRun;

var rescheduleCleaner = async function(){
  if(plannedRun)
    clearTimeout(plannedRun);
  delete plannedRun;

  var planNextRun = function(expires){
    expires = new Date(expires);
    var currentTime = new Date();
    var sleepMillis = expires.getTime() - currentTime.getTime();
    sleepMillis = Math.max(sleepMillis, 0);
    logger.verbose("Planning expired token cleanup for " + expires.toISOString());
    plannedRun = setTimeout(function(){
      return dbService.deleteExpiredTokens().then(function(){
        rescheduleCleaner();
      });
    }, sleepMillis);
  };

  var expires = await dbService.Token.min('expires');
  if(expires !== null && expires !== undefined && expires !== 0)
    planNextRun(expires);
};

module.exports.rescheduleCleaner = rescheduleCleaner;
{'fraction_non_alphanumeric': 0.1336661120354964, 'fraction_numerical': 0.0016638935108153079, 'mean_word_length': 4.290322580645161, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4592327', 'n_tokens_mistral': 533, 'n_tokens_neox': 522, 'n_words': 118}