|
@@ -12,7 +12,8 @@ class Job {
|
|
|
schedule = 'P1D',
|
|
|
repeat = false,
|
|
|
worker = false
|
|
|
- }) {
|
|
|
+ }, queue) {
|
|
|
+ this.queue = queue
|
|
|
this.finished = Promise.resolve()
|
|
|
this.name = name
|
|
|
this.immediate = immediate
|
|
@@ -27,6 +28,7 @@ class Job {
|
|
|
* @param {Object} data Job Data
|
|
|
*/
|
|
|
start(data) {
|
|
|
+ this.queue.jobs.push(this)
|
|
|
if (this.immediate) {
|
|
|
this.invoke(data)
|
|
|
} else {
|
|
@@ -82,16 +84,20 @@ class Job {
|
|
|
} catch (err) {
|
|
|
WIKI.logger.warn(err)
|
|
|
}
|
|
|
- if (this.repeat) {
|
|
|
+ if (this.repeat && this.queue.jobs.includes(this)) {
|
|
|
this.queue(data)
|
|
|
+ } else {
|
|
|
+ this.stop().catch(() => {})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Stop any future job invocation from occuring
|
|
|
*/
|
|
|
- stop() {
|
|
|
+ async stop() {
|
|
|
clearTimeout(this.timeout)
|
|
|
+ this.queue.jobs = this.queue.jobs.filter(x => x !== this)
|
|
|
+ return this.finished
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -118,16 +124,11 @@ module.exports = {
|
|
|
})
|
|
|
},
|
|
|
registerJob(opts, data) {
|
|
|
- const job = new Job(opts)
|
|
|
+ const job = new Job(opts, this)
|
|
|
job.start(data)
|
|
|
- if (job.repeat) {
|
|
|
- this.jobs.push(job)
|
|
|
- }
|
|
|
return job
|
|
|
},
|
|
|
- stop() {
|
|
|
- this.jobs.forEach(job => {
|
|
|
- job.stop()
|
|
|
- })
|
|
|
+ async stop() {
|
|
|
+ return Promise.all(this.jobs.map(job => job.stop()))
|
|
|
}
|
|
|
}
|