-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
99 lines (78 loc) · 1.94 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const pify = require('pify')
const config = require('./lib/config')
const templates = require('./lib/templates')
// Load translations
require('./lib/translations')
const notifier = module.exports = {}
/**
* Notifier Init
* Initialize async dependencies, including DB connection, jobs, etc
*
* @method init
* @return {Notifier}
*/
let initialization = null
notifier.init = function init () {
if (initialization) return initialization
initialization = Promise.all([
require('./lib/db'),
require('./lib/mailer'),
require('./lib/agenda')
]).then(([db, mailer, agenda]) => {
notifier.db = db
notifier.mailer = mailer
notifier.agenda = agenda
/**
* Promisified verions of Agenda#every, Agenda#schedule and
* Agenda#now methods
*/
;['every', 'schedule', 'now'].forEach((method) => {
notifier[method] = pify(agenda[method].bind(agenda))
})
return Promise.resolve(notifier)
}).then((notifier) => {
return require('./lib/jobs').init(notifier)
}).catch((err) => { throw err })
return initialization
}
/**
* Notifier Server Start
* Start processing jobs
*
* @method start
* @return {Notifier}
*/
notifier.start = function start () {
return notifier.init().then(() => {
notifier.agenda.start()
return Promise.resolve(notifier)
}).catch((err) => { throw err })
}
/**
* Expose config, this allows the overriding of any config option.
* @return {Config}
*/
notifier.config = config
/**
* Expose templates, this allows the overriding of any template.
* @return {Object}
*/
notifier.templates = templates
/**
* Expose db connection using mongojs
* Will be defined after the call of init()
* @return {MongoJS}
*/
notifier.db = null
/**
* Expose Agenda instance
* Will be defined after the call of init()
* @return {Agenda}
*/
notifier.agenda = null
/**
* Email sender utility
* Will be defined after the call of init()
* @return {Mailer}
*/
notifier.mailer = null