2019-02-18 02:48:48 +00:00
|
|
|
const moment = require('moment')
|
|
|
|
const childProcess = require('child_process')
|
2019-02-13 22:20:46 +00:00
|
|
|
const _ = require('lodash')
|
2019-02-17 06:32:35 +00:00
|
|
|
const configHelper = require('../helpers/config')
|
2019-02-13 22:20:46 +00:00
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
2019-02-18 02:48:48 +00:00
|
|
|
class Job {
|
|
|
|
constructor({
|
|
|
|
name,
|
|
|
|
immediate = false,
|
|
|
|
schedule = 'P1D',
|
|
|
|
repeat = false,
|
|
|
|
worker = false
|
2021-04-12 15:45:33 +00:00
|
|
|
}, queue) {
|
|
|
|
this.queue = queue
|
2019-02-18 02:48:48 +00:00
|
|
|
this.finished = Promise.resolve()
|
|
|
|
this.name = name
|
|
|
|
this.immediate = immediate
|
|
|
|
this.schedule = moment.duration(schedule)
|
|
|
|
this.repeat = repeat
|
|
|
|
this.worker = worker
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start Job
|
|
|
|
*
|
|
|
|
* @param {Object} data Job Data
|
|
|
|
*/
|
|
|
|
start(data) {
|
2021-04-12 15:45:33 +00:00
|
|
|
this.queue.jobs.push(this)
|
2019-02-18 02:48:48 +00:00
|
|
|
if (this.immediate) {
|
|
|
|
this.invoke(data)
|
|
|
|
} else {
|
2021-09-25 02:51:56 +00:00
|
|
|
this.enqueue(data)
|
2019-02-18 02:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue the next job run according to the wait duration
|
|
|
|
*
|
|
|
|
* @param {Object} data Job Data
|
|
|
|
*/
|
2021-09-25 02:51:56 +00:00
|
|
|
enqueue(data) {
|
2019-02-18 02:48:48 +00:00
|
|
|
this.timeout = setTimeout(this.invoke.bind(this), this.schedule.asMilliseconds(), data)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the actual job
|
|
|
|
*
|
|
|
|
* @param {Object} data Job Data
|
|
|
|
*/
|
|
|
|
async invoke(data) {
|
|
|
|
try {
|
|
|
|
if (this.worker) {
|
|
|
|
const proc = childProcess.fork(`server/core/worker.js`, [
|
|
|
|
`--job=${this.name}`,
|
|
|
|
`--data=${data}`
|
|
|
|
], {
|
2021-04-12 15:41:36 +00:00
|
|
|
cwd: WIKI.ROOTPATH,
|
|
|
|
stdio: ['inherit', 'inherit', 'pipe', 'ipc']
|
2019-02-18 02:48:48 +00:00
|
|
|
})
|
2022-05-16 05:13:42 +00:00
|
|
|
const stderr = []
|
2021-04-12 15:41:36 +00:00
|
|
|
proc.stderr.on('data', chunk => stderr.push(chunk))
|
2019-02-18 02:48:48 +00:00
|
|
|
this.finished = new Promise((resolve, reject) => {
|
|
|
|
proc.on('exit', (code, signal) => {
|
2021-04-12 15:41:36 +00:00
|
|
|
const data = Buffer.concat(stderr).toString()
|
2019-02-18 02:48:48 +00:00
|
|
|
if (code === 0) {
|
2021-04-12 15:41:36 +00:00
|
|
|
resolve(data)
|
2019-02-18 02:48:48 +00:00
|
|
|
} else {
|
2021-04-12 15:41:36 +00:00
|
|
|
const err = new Error(`Error when running job ${this.name}: ${data}`)
|
|
|
|
err.exitSignal = signal
|
|
|
|
err.exitCode = code
|
|
|
|
err.stderr = data
|
|
|
|
reject(err)
|
2019-02-18 02:48:48 +00:00
|
|
|
}
|
|
|
|
proc.kill()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.finished = require(`../jobs/${this.name}`)(data)
|
|
|
|
}
|
|
|
|
await this.finished
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.warn(err)
|
|
|
|
}
|
2021-04-12 15:45:33 +00:00
|
|
|
if (this.repeat && this.queue.jobs.includes(this)) {
|
2021-09-25 02:51:56 +00:00
|
|
|
this.enqueue(data)
|
2021-04-12 15:45:33 +00:00
|
|
|
} else {
|
|
|
|
this.stop().catch(() => {})
|
2019-02-18 02:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop any future job invocation from occuring
|
|
|
|
*/
|
2021-04-12 15:45:33 +00:00
|
|
|
async stop() {
|
2019-02-18 02:48:48 +00:00
|
|
|
clearTimeout(this.timeout)
|
2021-04-12 15:45:33 +00:00
|
|
|
this.queue.jobs = this.queue.jobs.filter(x => x !== this)
|
|
|
|
return this.finished
|
2019-02-18 02:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:20:46 +00:00
|
|
|
module.exports = {
|
|
|
|
jobs: [],
|
|
|
|
init() {
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
start() {
|
|
|
|
_.forOwn(WIKI.data.jobs, (queueParams, queueName) => {
|
2019-06-22 00:54:09 +00:00
|
|
|
if (WIKI.config.offline && queueParams.offlineSkip) {
|
|
|
|
WIKI.logger.warn(`Skipping job ${queueName} because offline mode is enabled. [SKIPPED]`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-07 04:06:47 +00:00
|
|
|
const schedule = (configHelper.isValidDurationString(queueParams.schedule)) ? queueParams.schedule : 'P1D'
|
2019-02-18 02:48:48 +00:00
|
|
|
this.registerJob({
|
|
|
|
name: _.kebabCase(queueName),
|
2019-10-07 04:06:47 +00:00
|
|
|
immediate: _.get(queueParams, 'onInit', false),
|
2019-02-18 02:48:48 +00:00
|
|
|
schedule: schedule,
|
2019-10-07 04:06:47 +00:00
|
|
|
repeat: _.get(queueParams, 'repeat', false),
|
|
|
|
worker: _.get(queueParams, 'worker', false)
|
2019-02-18 02:48:48 +00:00
|
|
|
})
|
2019-02-13 22:20:46 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
registerJob(opts, data) {
|
2021-04-12 15:45:33 +00:00
|
|
|
const job = new Job(opts, this)
|
2019-02-13 22:20:46 +00:00
|
|
|
job.start(data)
|
|
|
|
return job
|
|
|
|
},
|
2021-04-12 15:45:33 +00:00
|
|
|
async stop() {
|
|
|
|
return Promise.all(this.jobs.map(job => job.stop()))
|
2019-02-13 22:20:46 +00:00
|
|
|
}
|
|
|
|
}
|