<commit_before>var debug = require('debug')
var agenda = require('./lib/agenda')
var jobs = require('./lib/jobs')
var transports = require('./lib/transports')
var config = require('./lib/config')

var log = debug('democracyos:notifier')

var exports = module.exports = function startNotifier (opts, callback) {
  var inited = false

  config.set(opts)

  agenda = agenda({
    db: {
      address: config.get('mongoUrl'),
      collection: config.get('collection')
    }
  })

  transports()

  agenda.on('ready', () => {
    init((err) => {
      log('agenda initialized.')
      inited = true

      if (callback) {
        return callback(err)
      }
    })
  })

  agenda.on('error', (err) => {
    // throw the error to crash if the server is already started
    if (inited) throw err

    if (callback) {
      return callback(err)
    }
  })

  exports.notify = function notify (event, callback) {
    jobs.process(event.event, event, callback)
  }
}

function init (callback) {
  agenda.purge(function (err) {
    if (err) {
      if (callback) callback(err)
      return
    }

    // initialize job processors
    jobs.init(agenda)

    agenda.on('start', function (job) {
      log('Job \'%s\' started', job.attrs.name)
    })

    agenda.on('success', function (job) {
      log('Job \'%s\' completed', job.attrs.name)
    })

    agenda.on('fail', function (err, job) {
      log('Job \'%s\' failed - reason: %s', job.attrs.name, err)
    })

    agenda.start()<commit_msg>fix initialize jobs before purge<commit_after>var debug = require('debug')
var agenda = require('./lib/agenda')
var jobs = require('./lib/jobs')
var transports = require('./lib/transports')
var config = require('./lib/config')

var log = debug('democracyos:notifier')

var exports = module.exports = function startNotifier (opts, callback) {
  var inited = false

  config.set(opts)

  agenda = agenda({
    db: {
      address: config.get('mongoUrl'),
      collection: config.get('collection')
    }
  })

  transports()

  agenda.on('ready', () => {
    init((err) => {
      log('agenda initialized.')
      inited = true

      if (callback) {
        return callback(err)
      }
    })
  })

  agenda.on('error', (err) => {
    // throw the error to crash if the server is already started
    if (inited) throw err

    if (callback) {
      return callback(err)
    }
  })

  exports.notify = function notify (event, callback) {
    jobs.process(event.event, event, callback)
  }
}

function init (callback) {
  // initialize job processors
  jobs.init(agenda)

  agenda.purge(function (err) {
    if (err) {
      if (callback) callback(err)
      return
    }

    agenda.on('start', function (job) {
      log('Job \'%s\' started', job.attrs.name)
    })

    agenda.on('success', function (job) {
      log('Job \'%s\' completed', job.attrs.name)
    })

    agenda.on('fail', function (err, job) {
      log('Job \'%s\' failed - reason: %s', job.attrs.name, err)
    })

    agenda.start()
{'fraction_non_alphanumeric': 0.16259618601538975, 'fraction_numerical': 0.0, 'mean_word_length': 2.668711656441718, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4238326', 'n_tokens_mistral': 980, 'n_tokens_neox': 956, 'n_words': 237}