<commit_before>const _ = require('underscore');
const db = require('./');
const async = require('async');
const Schema = require('mongoose').Schema;
const util = require('../lib/util');

const WorkSchema = new Schema({
  _id: {
    type: String,
    unique: true,
    required: true,
    default: db.generateId,
  },
  type: {
    type: String,
    trim: true,
    required: true,
  },
  created: {
    type: Date,
    required: true,
    default: Date.now,
  },
  updated: {
    type: Date,
  },
  status: {
    type: String,
    trim: true,
    required: true,
    default: 'waiting',
    'enum': ['waiting', 'started', 'done', 'error'],
  },
  data: Schema.Types.Mixed
});

const Work = db.model('Work', WorkSchema);
module.exports = Work;


Work.getTask = function getTask(query, callback) {
  const ASCENDING_ORDER = 1;

  if (typeof query == 'string')
    query = { type: query };
  query = _.extend(query, { status: 'waiting' });

  Work.findOneAndUpdate(query, { status: 'started' })
    .sort('created')
    .exec(function (err, task) {
      if (err) return callback(err);
      return callback(null, task, function complete(status, cb) {
        if (typeof status == 'function')
          cb = status, status = 'done';
        if (!task) return cb();
        Work.findByIdAndUpdate(task._id, { status: status }, cb);
      });
    });

};

Work.reset = function reset(query, outerCallback) {
  Work.find(query, function (err, tasks) {
    if (err) return outerCallback(err);
    async.mapSeries(tasks, function (task, innerCallback) {
      task.status = 'waiting';
      task.save(innerCallback);
    }, outerCallback);
  });
};

Work.runQueue = function runQueue(query, workFn, callback) {
  // workFn gets called with two args: (task, nextFn)<commit_msg>Remove dead code.<commit_after>const _ = require('underscore');
const db = require('./');
const async = require('async');
const Schema = require('mongoose').Schema;
const util = require('../lib/util');

const WorkSchema = new Schema({
  _id: {
    type: String,
    unique: true,
    required: true,
    default: db.generateId,
  },
  type: {
    type: String,
    trim: true,
    required: true,
  },
  created: {
    type: Date,
    required: true,
    default: Date.now,
  },
  updated: {
    type: Date,
  },
  status: {
    type: String,
    trim: true,
    required: true,
    default: 'waiting',
    'enum': ['waiting', 'started', 'done', 'error'],
  },
  data: Schema.Types.Mixed
});

const Work = db.model('Work', WorkSchema);
module.exports = Work;


Work.getTask = function getTask(query, callback) {
  if (typeof query == 'string')
    query = { type: query };
  query = _.extend(query, { status: 'waiting' });

  Work.findOneAndUpdate(query, { status: 'started' })
    .sort('created')
    .exec(function (err, task) {
      if (err) return callback(err);
      return callback(null, task, function complete(status, cb) {
        if (typeof status == 'function')
          cb = status, status = 'done';
        if (!task) return cb();
        Work.findByIdAndUpdate(task._id, { status: status }, cb);
      });
    });

};

Work.reset = function reset(query, outerCallback) {
  Work.find(query, function (err, tasks) {
    if (err) return outerCallback(err);
    async.mapSeries(tasks, function (task, innerCallback) {
      task.status = 'waiting';
      task.save(innerCallback);
    }, outerCallback);
  });
};

Work.runQueue = function runQueue(query, workFn, callback) {
  // workFn gets called with two args: (task, nextFn)
{'fraction_non_alphanumeric': 0.1621082621082621, 'fraction_numerical': 0.0002849002849002849, 'mean_word_length': 3.0873108265424913, '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': '6090377', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1118, 'n_words': 298}