mirror of https://github.com/requarks/wiki
parent
aa27554bc7
commit
7e458f98b4
@ -1,84 +0,0 @@
|
|||||||
const moment = require('moment')
|
|
||||||
const childProcess = require('child_process')
|
|
||||||
|
|
||||||
module.exports = class Job {
|
|
||||||
constructor({
|
|
||||||
name,
|
|
||||||
immediate = false,
|
|
||||||
schedule = 'P1D',
|
|
||||||
repeat = false,
|
|
||||||
worker = false
|
|
||||||
}) {
|
|
||||||
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) {
|
|
||||||
if (this.immediate) {
|
|
||||||
this.invoke(data)
|
|
||||||
} else {
|
|
||||||
this.queue(data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Queue the next job run according to the wait duration
|
|
||||||
*
|
|
||||||
* @param {Object} data Job Data
|
|
||||||
*/
|
|
||||||
queue(data) {
|
|
||||||
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}`
|
|
||||||
], {
|
|
||||||
cwd: WIKI.ROOTPATH
|
|
||||||
})
|
|
||||||
this.finished = new Promise((resolve, reject) => {
|
|
||||||
proc.on('exit', (code, signal) => {
|
|
||||||
if (code === 0) {
|
|
||||||
resolve()
|
|
||||||
} else {
|
|
||||||
reject(signal)
|
|
||||||
}
|
|
||||||
proc.kill()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
this.finished = require(`../jobs/${this.name}`)(data)
|
|
||||||
}
|
|
||||||
await this.finished
|
|
||||||
} catch (err) {
|
|
||||||
WIKI.logger.warn(err)
|
|
||||||
}
|
|
||||||
if (this.repeat) {
|
|
||||||
this.queue(data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop any future job invocation from occuring
|
|
||||||
*/
|
|
||||||
stop() {
|
|
||||||
clearTimeout(this.timeout)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +1,20 @@
|
|||||||
|
const _ = require('lodash')
|
||||||
|
|
||||||
/* global WIKI */
|
/* global WIKI */
|
||||||
|
|
||||||
module.exports = async ({ target }) => {
|
module.exports = async (targetKey) => {
|
||||||
WIKI.logger.info(`Syncing with storage provider ${job.data.target.title}...`)
|
WIKI.logger.info(`Syncing with storage target ${targetKey}...`)
|
||||||
|
|
||||||
// try {
|
try {
|
||||||
// const target = require(`../modules/storage/${job.data.target.key}/storage.js`)
|
const target = _.find(WIKI.models.storage.targets, ['key', targetKey])
|
||||||
// target[job.data.event].call({
|
if (target) {
|
||||||
// config: job.data.target.config,
|
await target.fn.sync()
|
||||||
// mode: job.data.target.mode,
|
WIKI.logger.info(`Syncing with storage target ${targetKey}: [ COMPLETED ]`)
|
||||||
// page: job.data.page
|
} else {
|
||||||
// })
|
throw new Error('Invalid storage target. Unable to perform sync.')
|
||||||
// WIKI.logger.info(`Syncing with storage provider ${job.data.target.title}: [ COMPLETED ]`)
|
}
|
||||||
// } catch (err) {
|
} catch (err) {
|
||||||
// WIKI.logger.error(`Syncing with storage provider ${job.data.target.title}: [ FAILED ]`)
|
WIKI.logger.error(`Syncing with storage target ${targetKey}: [ FAILED ]`)
|
||||||
// WIKI.logger.error(err.message)
|
WIKI.logger.error(err.message)
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue