<commit_before>
'use strict';

const Queue    = require('idoit');
const inherits = require('util').inherits;


module.exports = function (N) {
  N.wire.on('init:jobs', function* register_search_reindex() {
    let reindex_tasks = [];

    yield N.wire.emit('internal:search.reindex.tasklist', reindex_tasks);

    if (!N.config.cron || !N.config.cron.search_reindex) {
      return new Error('No config defined for cron task "search_reindex"');
    }

    function ReindexTemplate(queue, args) {
      let tasks = [];

      tasks.push(N.queue.search_reindex_start());

      reindex_tasks.forEach(name => {
        if (args && args.cutoff) {
          let cutoff_ts = Date.now() - args.cutoff * 24 * 60 * 60 * 1000;
          let cutoff_objectid = Math.floor(cutoff_ts / 1000).toString(16) + '0000000000000000';

          tasks.push(N.queue[name](cutoff_objectid));
        } else {
          tasks.push(N.queue[name]());
        }
      });

      if (N.config.search.optimize) {
        tasks.push(N.queue.search_optimize());
      }

      tasks.push(N.queue.search_reindex_done());

      Queue.ChainTemplate.call(this, queue, tasks);
    }

    inherits(ReindexTemplate, Queue.ChainTemplate);

    ReindexTemplate.serializableFields = Queue.ChainTemplate.serializableFields;

    ReindexTemplate.extend = function (options) {
      class T extends ReindexTemplate {}

      Object.assign(T.prototype, options);

      return T;
    };


    N.queue.registerTask({
      name: 'search_reindex',
      pool: 'hard',
      baseClass: ReindexTemplate,
      cron: N.config.cron.search_reindex,
      taskID: () => 'search_reindex'
    });


    N.queue.registerTask({
      name: 'search_reindex_start',
      pool: 'hard',
      process: () => N.search.reindex_start(N.config.search)
    });


    N.queue.registerTask({
      name: 'search_reindex_done',
      pool: 'hard',
      process: () => N.search.reindex_done()
    });


    N.queue.on('task:progress:search_reindex', function (task_info) {
      N.live.debounce('admin.search.index.reindex', {
        uid:     task_info.uid,
        current: task_info.progress,
        total:   task_info.total<commit_msg>Rewrite tasks to remove unnecessary subclassing<commit_after>
'use strict';


const Promise  = require('bluebird');
const Queue    = require('idoit');


module.exports = function (N) {
  N.wire.on('init:jobs', function register_search_reindex() {

    if (!N.config.cron || !N.config.cron.search_reindex) {
      return new Error('No config defined for cron task "search_reindex"');
    }

    N.queue.registerTask({
      name: 'search_reindex',
      pool: 'hard',
      baseClass: Queue.ChainTemplate,
      cron: N.config.cron.search_reindex,
      taskID: () => 'search_reindex',
      init: Promise.coroutine(function* () {
        let task_names = [];
        let tasks = [];

        yield N.wire.emit('internal:search.reindex.tasklist', task_names);

        tasks.push(N.queue.search_reindex_start());

        task_names.forEach(name => {
          if (this.args[0] && this.args[0].cutoff) {
            let cutoff_ts = Date.now() - this.args[0].cutoff * 24 * 60 * 60 * 1000;
            let cutoff_objectid = Math.floor(cutoff_ts / 1000).toString(16) + '0000000000000000';

            tasks.push(N.queue[name](cutoff_objectid));
          } else {
            tasks.push(N.queue[name]());
          }
        });

        if (N.config.search.optimize) {
          tasks.push(N.queue.search_optimize());
        }

        tasks.push(N.queue.search_reindex_done());

        this.__children_to_init__ = tasks;
      })
    });


    N.queue.registerTask({
      name: 'search_reindex_start',
      pool: 'hard',
      process: () => N.search.reindex_start(N.config.search)
    });


    N.queue.registerTask({
      name: 'search_reindex_done',
      pool: 'hard',
      process: () => N.search.reindex_done()
    });


    N.queue.on('task:progress:search_reindex', function (task_info) {
      N.live.debounce('admin.search.index.reindex', {
        uid:     task_info.uid,
        current: task_info.progress,
        total:   task_info.total
{'fraction_non_alphanumeric': 0.14555582465488012, 'fraction_numerical': 0.01622668927100993, 'mean_word_length': 2.6132983377077865, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1866110', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1312, 'n_words': 217}