-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
280 lines (247 loc) · 7.35 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import kue from 'kue'
import later from 'later'
import assert from 'assert'
import zmq from 'zeromq'
import { promisify } from 'bluebird'
import { merge, omit } from 'lodash'
import Logger from './logger'
import Mailer from './mailer'
import { parseBackoff } from './util'
const { NODE_ENV } = process.env
/**
* Default scheduler config
*/
const defaultConfig = {
jobs: [],
options: {
port: 5555,
logging: { path: 'log' },
mailer: null
}
}
/**
* Promisified version of `kue.Job.rangeByType`
* @type {Function}
*/
const getJobsByType = promisify(kue.Job.rangeByType)
/**
* Promisified version of `kue.Job.get`
*
* @type {Function}
*/
export const getJob = promisify(kue.Job.get)
/**
* Dispo Scheduler
*/
export default class Dispo {
/**
* Creates an instance of Dispo.
*
* @memberOf Dispo
* @param {Object} [config={}]
*/
constructor (config = {}) {
this.config = merge({}, defaultConfig, config)
}
/**
* Initializes logging, socket bindings and the queue mechanism
*
* @memberOf Dispo
* @return {Promise<void>}
*/
async init () {
this._logger = new Logger(this.config.options.logging)
this._logger.init()
if (this.config.options.mailer) {
this._mailer = new Mailer(this.config.options.mailer)
this._mailer.init()
}
this._initSocket()
this._initQueue(this.config.options.queue)
for (let job of this.config.jobs) {
await this.defineJob(job)
}
}
/**
* @typedef {Object} DefineJobOptions
* @property {String} name - Job name
* @property {Function} fn - Job method that is executed when the job is run
* @property {Number} attempts - Number of attempts a job is retried until marked as failure
* @property {String} cron - Interval-based scheduling written in cron syntax, ignored when delay is given
*/
/**
* Defines a job
*
* @memberOf Dispo
* @param {DefineJobOptions} options - Job options
* @return {Promise<void>}
*/
async defineJob ({ attempts, cron, notifyOnError, fn, name, backoff }) {
assert(name, 'Job must have a name')
const options = { attempts, backoff }
this._queue.process(name, (job, done) => fn(job).then(done, done))
if (notifyOnError) {
options.notifyOnError = notifyOnError
}
if (cron) {
options.cron = cron
await this._queueJob(name, options)
}
}
/**
* Initializes the queue mechanism
*
* This is mostly done to set up queue level logging and to be able to automatically
* queue the next runs of cronjobs after their previous runs have completed.
*
* @memberOf Dispo
* @param {Object} [options={}]
*/
_initQueue (options = {}) {
this._queue = kue.createQueue(options)
this._queue.watchStuckJobs(5e3)
if (NODE_ENV !== 'test') {
this._queue.on('job start', async (id) => await this._handleStart(id))
this._queue.on('job failed attempt', async (id, msg) => await this._handleFailedAttempt(id, msg))
this._queue.on('job failed', async (id, msg) => await this._handleFailed(id, msg))
}
this._queue.on('job complete', async (id) => await this._handleComplete(id))
}
/**
* Logs job starts
*
* @memberOf Dispo
* @param {Number} id - Job id
*/
async _handleStart (id) {
await this._logger.logStart(id)
}
/**
* Logs failed attempts
*
* @memberOf Dispo
* @param {Number} id - Job id
* @param {String} msg - Error message
*/
async _handleFailedAttempt (id, msg) {
await this._logger.logFailedAttempt(id, msg)
}
/**
* Logs failed jobs and sends notification emails if configured to do so
*
* @memberOf Dispo
* @param {Number} id - Job id
* @param {String} msg - Error message
*/
async _handleFailed (id, msg) {
await this._logger.logFailure(id, msg)
const job = await getJob(id)
if (this._mailer) {
await this._mailer.sendMail(id, job.error())
}
}
/**
* Logs completed jobs and re-queues them when defined as a cron
*
* @memberOf Dispo
* @param {Number} id - Job id
*/
async _handleComplete (id) {
if (NODE_ENV !== 'test') {
await this._logger.logComplete(id)
}
const job = await getJob(id)
if (job.data.cron) {
await this._queueJob(job.data.name, job.data)
}
}
/**
* Initialize ØMQ reply socket
*
* Received messages add new jobs to the queue when the given job is defined in
* the job configuration
*
* @memberOf Dispo
* @param {Number|String} [port=this.config.options.port]
*/
_initSocket (port = this.config.options.port) {
const responder = zmq.socket('rep')
responder.on('message', async (message) => {
const payload = JSON.parse(message.toString())
const job = this.config.jobs.filter((job) => job.name === payload.name).shift()
if (job) {
const data = omit(Object.assign(payload, job), 'fn', 'name')
await this._queueJob(job.name, data)
responder.send('ok')
}
})
responder.bind(`tcp://*:${port}`, (err) => {
if (err) {
throw new Error(`Port binding: ${err.message}`)
} else if (NODE_ENV !== 'test') {
this._logger.verbose(`ZeroMQ rep socket listening on port ${port}`)
}
})
}
/**
* Checks if a cronjob of the given `name` is already scheduled.
*
* @memberOf Dispo
* @param {String} name - The jobs name
* @return {Promise<Boolean>}
*/
async _isCronScheduled (name) {
const jobsByType = await getJobsByType(name, 'delayed', 0, 10000, 'desc')
const cronjobsByType = jobsByType.filter((job) => !!job.data.cron)
return cronjobsByType.length > 0
}
/**
* @typedef {Object} QueueJobOptions
* @property {Number} attempts - Number of attempts a job is retried until marked as failure
* @property {Number} delay - Delay job run by the given amount of miliseconds
* @property {String} cron - Interval-based scheduling written in cron syntax, ignored when delay is given
* @property {Boolean|{type:String,delay:Number}} backoff - Interval-based scheduling written in cron syntax, ignored when delay is given
*/
/**
* Queues a job.
*
* @memberOf Dispo
* @param {String} name - Job name
* @param {QueueJobOptions} options - Job options
* @return {Promise<void>}
*/
async _queueJob (name, options) {
const { attempts, cron, delay, backoff } = options
assert(!!cron || !!delay, 'To queue a job, either `cron` or `delay` needs to be defined')
const isScheduled = await this._isCronScheduled(name)
if (!cron || !isScheduled) {
const job = this._queue.create(name, Object.assign(options, { name }))
.delay(delay || this._calculateDelay(cron))
.attempts(attempts)
if (backoff) {
console.log(name, backoff)
job.backoff(parseBackoff(backoff))
}
job.save((err) => {
if (err) {
throw new Error(`Job save: ${err.message}`)
} else if (NODE_ENV !== 'test') {
this._logger.logQueued(job)
}
})
}
}
/**
* Calculates the delay until a cronjobs next run is due
*
* @memberOf Dispo
* @param {String} cron - Interval-based scheduling written in cron syntax
* @return {Number} Number of miliseconds until next cron run
*/
_calculateDelay (cron) {
return later.schedule(later.parse.cron(cron)).next(2)
.map((date) => date.getTime() - Date.now())
.filter((msec) => msec > 500)
.shift()
}
}