forked from DemocracyOS/notifier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
74 lines (61 loc) · 1.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var defaults = require('defaults-deep')
var agenda = require('./lib/agenda')
var timing = require('./lib/utils/timing')
var jobs = require('./lib/jobs')
var transports = require('./lib/transports')
var log = require('debug')('democracyos:notifier')
var defaultOpts = {
mongoUrl: 'mongodb://localhost/DemocracyOS-dev',
collection: 'notifierJobs',
defaultLocale: 'en',
organizationName: 'noreply@democracyos.org',
organizationEmail: 'The DemocracyOS team',
mailer: {
service: '',
auth: {
user: '',
pass: ''
}
}
}
var exports = module.exports = function startNotifier(opts, callback) {
defaults(opts, defaultOpts)
agenda = agenda({
db: {
address: opts.mongoUrl,
collection: opts.collection
}
})
transports = transports(opts)
setTimeout(function verifyInitialization(){
if (!agenda._collection) {
log('initializing agenda...')
return setTimeout(verifyInitialization, 100)
}
log('agenda initialized.')
init(opts, callback)
}, 100)
exports.notify = function notify(event, callback) {
jobs.process(event.event, event, callback)
}
}
function init(opts, callback){
agenda.purge(function (err) {
if (err) return callback && callback(err)
//initialize job processors
jobs(agenda, opts.mongoUrl)
agenda.on('start', function (job) {
timing.start(job)
log('Job \'%s\' started', job.attrs.name)
})
agenda.on('success', function (job) {
var duration = timing.finish(job)
log('Job \'%s\' completed - duration: %s', job.attrs.name, duration.asMilliseconds())
})
agenda.on('fail', function (err, job) {
log('Job \'%s\' failed - reason: %s', job.attrs.name, err)
})
agenda.start()
if (callback) return callback()
})
}