<commit_before>/**
 * The main class and interface of the schedule module
 */
var PriorityQueue = require('./priorityQueue');
var Job = require('./job.js');
var timerCount = 0;

var logger = require('log4js').getLogger(__filename);

var map = {};
var queue = PriorityQueue.createPriorityQueue(comparator);

var jobId = 0;
var timer;

//The accuracy of the scheduler, it will affect the performance when the schedule tasks are
//crowded together
var accuracy = 10;

/**
 * Schedule a new Job
 */
function scheduleJob(trigger, jobFunc, jobData){
  var job = Job.createJob(trigger, jobFunc, jobData);
  var excuteTime = job.excuteTime();
  var id = job.id;

  map[id] = job;
  var element = {
    id : id,
    time : excuteTime
  };

  var curJob = queue.peek();
  if(!curJob || excuteTime < curJob.time){
    queue.offer(element);
    setTimer(job);

    return job.id;
  }

  queue.offer(element);
  return job.id;
}

/**
 * Cancel Job
 */
function cancelJob(id){
  if(id == queue.peek().id){
    queue.pop();
    delete map[id];

    var job = getNextJob();
    if(!job)
      return true;

    setTimer(job);
  }
  delete map[id];
  return true;
}

/**
 * Clear last timeout and schedule the next job, it will automaticly run the job that
 * need to run now
 * @param job The job need to schedule
 * @return void
 */
function setTimer(job){
  clearTimeout(timer);

  while((job.time-Date.now()) < accuracy){
    job.run();
    job = getNextJob();
    if(!job)
      return;
  }

  timer = setTimeout(excuteJob, job.excuteTime()-Date.now());<commit_msg>schedule refactor 1.job.run() only in excuteJob(), cancelJob() pop the job, then call excuteJob(), setTimer() only setTimeout the next job<commit_after>/**
 * The main class and interface of the schedule module
 */
var PriorityQueue = require('./priorityQueue');
var Job = require('./job.js');
var timerCount = 0;

var logger = require('log4js').getLogger(__filename);

var map = {};
var queue = PriorityQueue.createPriorityQueue(comparator);

var jobId = 0;
var timer;

//The accuracy of the scheduler, it will affect the performance when the schedule tasks are
//crowded together
var accuracy = 10;

/**
 * Schedule a new Job
 */
function scheduleJob(trigger, jobFunc, jobData){
  var job = Job.createJob(trigger, jobFunc, jobData);
  var excuteTime = job.excuteTime();
  var id = job.id;

  map[id] = job;
  var element = {
    id : id,
    time : excuteTime
  };

  var curJob = queue.peek();
  if(!curJob || excuteTime < curJob.time){
    queue.offer(element);
    setTimer(job);

    return job.id;
  }

  queue.offer(element);
  return job.id;
}

/**
 * Cancel Job
 */
function cancelJob(id){
  if(id == queue.peek().id){
    queue.pop();
    delete map[id];

    clearTimeout(timer);
    excuteJob();
  }
  delete map[id];
  return true;
}

/**
 * Clear last timeout and schedule the next job, it will automaticly run the job that
 * need to run now
 * @param job The job need to schedule
 * @return void
 */
function setTimer(job){
  clearTimeout(timer);

  timer = setTimeout(excuteJob, job.excuteTime()-Date.now());
{'fraction_non_alphanumeric': 0.13770704774277362, 'fraction_numerical': 0.0035725885027606367, 'mean_word_length': 3.4444444444444446, 'pattern_counts': {'":': 0, '<': 6, '<?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': '1149419', 'n_tokens_mistral': 1015, 'n_tokens_neox': 986, 'n_words': 311}