wikijs-fork/server/core/queue.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

const path = require('path')
2017-08-03 03:47:11 +00:00
const Bull = require('bull')
const Promise = require('bluebird')
const _ = require('lodash')
2017-08-03 03:47:11 +00:00
/* global WIKI */
2017-08-03 03:47:11 +00:00
module.exports = {
job: {},
2017-08-03 03:47:11 +00:00
init() {
_.forOwn(WIKI.data.jobs, (queueParams, queueName) => {
this.job[queueName] = new Bull(queueName, {
prefix: `q-${WIKI.config.ha.uid}`,
redis: WIKI.config.redis
2017-08-03 03:47:11 +00:00
})
this.job[queueName].process(path.join(WIKI.SERVERPATH, `jobs/${_.kebabCase(queueName)}.js`))
2017-08-03 03:47:11 +00:00
})
return this
},
start() {
_.forOwn(WIKI.data.jobs, (queueParams, queueName) => {
if (queueParams.onInit) {
this.job[queueName].add({}, {
removeOnComplete: true
})
}
if (queueParams.cron) {
this.job[queueName].add({}, {
repeat: { cron: queueParams.cron },
removeOnComplete: true
})
}
})
},
async clean() {
return Promise.each(_.keys(WIKI.data.jobs), queueName => {
2017-08-03 03:47:11 +00:00
return new Promise((resolve, reject) => {
let keyStream = WIKI.redis.scanStream({
match: `q-${WIKI.config.ha.uid}:${queueName}:*`
2017-08-03 03:47:11 +00:00
})
keyStream.on('data', resultKeys => {
if (resultKeys.length > 0) {
WIKI.redis.del(resultKeys)
2017-08-03 03:47:11 +00:00
}
})
keyStream.on('end', resolve)
})
}).then(() => {
WIKI.logger.info('Purging old queue jobs: [ OK ]')
2017-10-08 02:44:35 +00:00
}).return(true).catch(err => {
WIKI.logger.error(err)
2017-08-03 03:47:11 +00:00
})
}
}