<commit_before>const { deepGet, getTimeStamp, tryJSONParse } = require('redibox');
const Promise = require('bluebird');
const EventEmitter = require('eventemitter3');

const Job = require('./job');
const defaults = require('./defaults');

/**
 * TODO move to helpers
 * @param errorStack
 * @returns {Array}
 */
function trimStack(errorStack) {
  const oldStack = errorStack.split('\n');
  const stack = [];
  for (let i = 0, iLen = oldStack.length || stack.length > 19; i < iLen; i++) {
    const row = oldStack[i];

    // include private modules
    if (row.includes('@')) {
      stack.push(row);
      continue;
    }

    // exclude job module
    if (row.includes('redibox-hook-job') || row.includes('redibox/job/lib')) continue;

    // exclude bluebird
    if (row.includes('bluebird')) continue;

    // exclude waterline
    if (row.includes('waterline/lib')) continue;

    // exclude async
    if (row.includes('async/lib')) continue;

    // exclude timers.js
    if (row.includes('timers.js:')) continue;

    stack.push(row);
  }

  return stack;
}

module.exports = class Queue extends EventEmitter {

  /**
   *
   * @param options
   * @param core
   * @returns {Queue}
   */
  constructor(options, core) {
    super();
    this.core = core;
    this.paused = false;
    this.started = false;
    this.throttled = false;
    this.log = this.core.log;
    this.name = options.name;
    this.client = core.client;
    this.handler = options.handler || null;
    this.options = Object.assign({}, defaults.queue, options || {});
    this.core.createClient('block', this);
    this.handlerTracker = {};
  }

  getJobById(id, cb) {
    return this.client
      .hget(this.toKey('jobs'), id, (error, job) => {
        cb(error, this.getJobInstance(job));
      });
  }

  getJobInstance(job) {
    const _job = tryJSONParse(job);
    if (!_job) return null;

    return new Job(this.core, this.name, _job.options);
  }

  start() {
    if (!this.started) {
      this.paused = false;
      this.beginWorking.bind(this)();
    }
  }

  stop() {<commit_msg>Add comment blocks<commit_after>const { deepGet, getTimeStamp, tryJSONParse } = require('redibox');
const Promise = require('bluebird');
const EventEmitter = require('eventemitter3');

const Job = require('./job');
const defaults = require('./defaults');

/**
 * TODO move to helpers
 * @param errorStack
 * @returns {Array}
 */
function trimStack(errorStack) {
  const oldStack = errorStack.split('\n');
  const stack = [];
  for (let i = 0, iLen = oldStack.length || stack.length > 19; i < iLen; i++) {
    const row = oldStack[i];

    // include private modules
    if (row.includes('@')) {
      stack.push(row);
      continue;
    }

    // exclude job module
    if (row.includes('redibox-hook-job') || row.includes('redibox/job/lib')) continue;

    // exclude bluebird
    if (row.includes('bluebird')) continue;

    // exclude waterline
    if (row.includes('waterline/lib')) continue;

    // exclude async
    if (row.includes('async/lib')) continue;

    // exclude timers.js
    if (row.includes('timers.js:')) continue;

    stack.push(row);
  }

  return stack;
}

module.exports = class Queue extends EventEmitter {

  /**
   *
   * @param options
   * @param core
   * @returns {Queue}
   */
  constructor(options, core) {
    super();
    this.core = core;
    this.paused = false;
    this.started = false;
    this.throttled = false;
    this.log = this.core.log;
    this.name = options.name;
    this.client = core.client;
    this.handler = options.handler || null;
    this.options = Object.assign({}, defaults.queue, options || {});
    this.core.createClient('block', this);
    this.handlerTracker = {};
  }

  /**
   * Returns a job by ID from Redis
   * @param id
   * @param cb
   * @returns {*}
   */
  getJobById(id, cb) {
    return this.client
      .hget(this.toKey('jobs'), id, (error, job) => {
        cb(error, this.getJobInstance(job));
      });
  }

  /**
   * Returns an instance of a Job
   * @param job
   * @returns {null}
   */
  getJobInstance(job) {
    const _job = tryJSONParse(job);
    if (!_job) return null;

    return new Job(this.core, this.name, _job.options);
  }

  /**
   * Start the queue
   */
  start() {
    if (!this.started) {
      this.paused = false;
      this.beginWorking.bind(this)();
    }
  }

  /**
   * Stop/pause the queue
   */
  stop() {
{'fraction_non_alphanumeric': 0.1622237411711096, 'fraction_numerical': 0.001822738664843928, 'mean_word_length': 2.72982158028887, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '172350', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1429, 'n_words': 332}