<commit_before>/**
 * Module to abstract runnable tasks with error context wrapping and timeouts.
 * @module task
 */

'use strict';

var DEFAULT_TIMEOUT = 30 * 1000;

/**
 * Constructor for Task class.
 * @class
 * @arg {string} description - Name or description of task.
 * @arg {number} timeout [default=30,000] - Timeout in milliseconds.
 * @arg {function} cmd - The task to run.
 */
function Task(description, timeoutDuration, cmd) {
  this._description = description;
  if (typeof cmd === 'undefined') {
    cmd = timeoutDuration;
    timeoutDuration = DEFAULT_TIMEOUT;
  }
  if (typeof cmd !== 'function') {
    var msg1 = 'Command passed to task [' + cmd + '] is not a function.';
    var err1 = this._wrapError(new Error(msg1));
    throw err1;
  }
  if (typeof timeoutDuration !== 'number') {
    var msg2 = 'Timeout passed to task [' + timeoutDuration + '] is not a number.';
    var err2 = this._wrapError(new Error(msg2));
    throw err2;
  }
  this._cmd = cmd;
  this._timeoutDuration = timeoutDuration;
}

/**
 * Gets the task description.
 * @returns {string} description
 */
Task.prototype.getDescription = function() {
  return this._description;
};

/**
 * Gets the task timeout duration.
 * @returns {number} timeout duration
 */
Task.prototype.getTimeout = function() {
  return this._timeoutDuration;
};

/**
 * Sets the task timeout duration.
 * @arg {number} timeout duration
 */
Task.prototype.setTimeout = function(timeoutDuration) {
  this._timeoutDuration = timeoutDuration;
};

/**
 * Gets the task command function.
 * @returns {function} command function
 */
Task.prototype.getCommand = function() {
  return this._cmd;
};

Task.prototype._wrapError = function(err) {
  var context = 'While running task [' + this._description + ']';
  return new Error(context + ' ' + err);
};

/**
 * Run the task command function.
 * @arg {function} callback - Function to be called back after task is complete.
 * @example
 * var Task = require(...);
 *
 * var cmd = function(done) {
 *   // Do some stuff.
 *   // The done() function MUST to be called so the task timeout can be canceled.
 *   done();
 * };
 * var timeout = 15 * 1000; // 15 seconds
 * var task = new Task('mytask', timeout, cmd);
 *
 * // Run task with a callback.
 * task.run(function(err) {
 *   // Err will be a wrapped error with context info added.
 *   if (err) throw err
 * });
 *
 * // Or you can listen for callback events.
 * task.on('end', function() {
 *   console.log('Task is complete!');
 * });
 * task.on('error', function(err) {
 *   // Err will be a wrapped error with context info added.
 *   throw err
 * });
 * task.on('timeout', function(timeoutErr) {
 *   // Err will be a wrapped error with context info added.
 *   throw err
 * });
 * task.run();
 */
Task.prototype.run = function(callback) {
  // Setup callbacks.
  var cbError = callback || this._cbError;
  var cbEnd = callback || this._cbEnd;
  var cbTimeout = callback || this._cbTimeout;

  // So we can access 'this' in a different scope.
  var self = this;

  // Start a timeout.
  var timeout = setTimeout(function() {
    var timeoutErr = new Error('Task timed out after ' +
      self._timeoutDuration + 'ms.');
    if (cbTimeout) {
      cbTimeout(self._wrapError(timeoutErr));
    }
  }, this._timeoutDuration);

  // Run the task.
  this._cmd(function(err) {
    // Clear the timeout.
    clearTimeout(timeout);

    // Call the callbacks.
    if (err && cbError) {
      cbError(self._wrapError(err));
    } else if (cbEnd) {
      cbEnd(null);
    }
  });
};

/**
 * Registers a callback event.
 * @arg {string} eventName - (error|end|timeout)
 * @arg {function} callback - Callback function to be called on event<commit_msg>Disabling timeouts for tasks for now.<commit_after>/**
 * Module to abstract runnable tasks with error context wrapping and timeouts.
 * @module task
 */

'use strict';

var DEFAULT_TIMEOUT = 30 * 1000;

/**
 * Constructor for Task class.
 * @class
 * @arg {string} description - Name or description of task.
 * @arg {number} timeout [default=30,000] - Timeout in milliseconds.
 * @arg {function} cmd - The task to run.
 */
function Task(description, timeoutDuration, cmd) {
  this._description = description;
  if (typeof cmd === 'undefined') {
    cmd = timeoutDuration;
    timeoutDuration = DEFAULT_TIMEOUT;
  }
  if (typeof cmd !== 'function') {
    var msg1 = 'Command passed to task [' + cmd + '] is not a function.';
    var err1 = this._wrapError(new Error(msg1));
    throw err1;
  }
  if (typeof timeoutDuration !== 'number') {
    var msg2 = 'Timeout passed to task [' + timeoutDuration + '] is not a number.';
    var err2 = this._wrapError(new Error(msg2));
    throw err2;
  }
  this._cmd = cmd;
  this._timeoutDuration = timeoutDuration;
}

/**
 * Gets the task description.
 * @returns {string} description
 */
Task.prototype.getDescription = function() {
  return this._description;
};

/**
 * Gets the task timeout duration.
 * @returns {number} timeout duration
 */
Task.prototype.getTimeout = function() {
  return this._timeoutDuration;
};

/**
 * Sets the task timeout duration.
 * @arg {number} timeout duration
 */
Task.prototype.setTimeout = function(timeoutDuration) {
  this._timeoutDuration = timeoutDuration;
};

/**
 * Gets the task command function.
 * @returns {function} command function
 */
Task.prototype.getCommand = function() {
  return this._cmd;
};

Task.prototype._wrapError = function(err) {
  var context = 'While running task [' + this._description + ']';
  return new Error(context + ' ' + err);
};

/**
 * Run the task command function.
 * @arg {function} callback - Function to be called back after task is complete.
 * @example
 * var Task = require(...);
 *
 * var cmd = function(done) {
 *   // Do some stuff.
 *   // The done() function MUST to be called so the task timeout can be canceled.
 *   done();
 * };
 * var timeout = 15 * 1000; // 15 seconds
 * var task = new Task('mytask', timeout, cmd);
 *
 * // Run task with a callback.
 * task.run(function(err) {
 *   // Err will be a wrapped error with context info added.
 *   if (err) throw err
 * });
 *
 * // Or you can listen for callback events.
 * task.on('end', function() {
 *   console.log('Task is complete!');
 * });
 * task.on('error', function(err) {
 *   // Err will be a wrapped error with context info added.
 *   throw err
 * });
 * task.on('timeout', function(timeoutErr) {
 *   // Err will be a wrapped error with context info added.
 *   throw err
 * });
 * task.run();
 */
Task.prototype.run = function(callback) {
  // Setup callbacks.
  var cbError = callback || this._cbError;
  var cbEnd = callback || this._cbEnd;
  var cbTimeout = callback || this._cbTimeout;

  // So we can access 'this' in a different scope.
  var self = this;

  // Start a timeout.
  /*var timeout = setTimeout(function() {
    var timeoutErr = new Error('Task timed out after ' +
      self._timeoutDuration + 'ms.');
    if (cbTimeout) {
      cbTimeout(self._wrapError(timeoutErr));
    }
  }, this._timeoutDuration);*/

  // Run the task.
  this._cmd(function(err) {
    // Clear the timeout.
    //clearTimeout(timeout);

    // Call the callbacks.
    if (err && cbError) {
      cbError(self._wrapError(err));
    } else if (cbEnd) {
      cbEnd(null);
    }
  });
};

/**
 * Registers a callback event.
 * @arg {string} eventName - (error|end|timeout)
 * @arg {function} callback - Callback function to be called on event
{'fraction_non_alphanumeric': 0.14080150618612158, 'fraction_numerical': 0.007261968800430339, 'mean_word_length': 3.10657095527333, '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': '5057652', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2224, 'n_words': 740}