refactoring taskManager
This commit is contained in:
@@ -12,7 +12,6 @@ const mail = {
|
|||||||
log.debug('Enqueue new email ', template, locale)
|
log.debug('Enqueue new email ', template, locale)
|
||||||
const task = new Task({
|
const task = new Task({
|
||||||
name: 'MAIL',
|
name: 'MAIL',
|
||||||
removable: true,
|
|
||||||
method: mail._send,
|
method: mail._send,
|
||||||
args: [addresses, template, locals, locale]
|
args: [addresses, template, locals, locale]
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -4,17 +4,17 @@ const placeHelpers = require('./helpers/place')
|
|||||||
const tagHelpers = require('./helpers/tag')
|
const tagHelpers = require('./helpers/tag')
|
||||||
// const notifier = require('./notifier')
|
// const notifier = require('./notifier')
|
||||||
|
|
||||||
const loopInterval = process.env.NODE_ENV === 'production' ? 15 : 1
|
const loopInterval = 1 // process.env.NODE_ENV === 'production' ? 1 : 1
|
||||||
const minute = 60 / loopInterval
|
const minute = 60 / loopInterval
|
||||||
const hour = minute * 60
|
const hour = minute * 60
|
||||||
const day = hour * 24
|
const day = hour * 24
|
||||||
|
|
||||||
class Task {
|
class Task {
|
||||||
constructor ({ name, removable = false, repeatEach = 1, method, args = [] }) {
|
constructor ({ name, repeat = false, repeatDelay = 1, callAtStart = false, method, args = [] }) {
|
||||||
this.name = name
|
this.name = name
|
||||||
this.removable = removable
|
this.repeat = repeat
|
||||||
this.repeatEach = repeatEach
|
this.repeatDelay = repeatDelay
|
||||||
this.processInNTick = repeatEach
|
this.processInNTick = callAtStart ? 0 : repeatDelay
|
||||||
this.method = method
|
this.method = method
|
||||||
this.args = args
|
this.args = args
|
||||||
}
|
}
|
||||||
@@ -24,7 +24,7 @@ class Task {
|
|||||||
if (this.processInNTick > 0) {
|
if (this.processInNTick > 0) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.processInNTick = this.repeatEach
|
this.processInNTick = this.repeatDelay
|
||||||
try {
|
try {
|
||||||
const ret = this.method.apply(this, this.args)
|
const ret = this.method.apply(this, this.args)
|
||||||
if (ret && typeof ret.then === 'function') {
|
if (ret && typeof ret.then === 'function') {
|
||||||
@@ -69,7 +69,7 @@ class TaskManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
add (task) {
|
add (task) {
|
||||||
log.debug(`[TASK] Add ${task.name} (${task.repeatEach * this.interval} seconds)`)
|
log.debug(`[TASK] Add ${task.name} (${task.repeatDelay * this.interval} seconds)`)
|
||||||
this.tasks.push(task)
|
this.tasks.push(task)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +82,7 @@ class TaskManager {
|
|||||||
const tasks = this.tasks.map(t => t.process())
|
const tasks = this.tasks.map(t => t.process())
|
||||||
|
|
||||||
// remove removable tasks
|
// remove removable tasks
|
||||||
this.tasks = this.tasks.filter(t => !t.removable)
|
this.tasks = this.tasks.filter(t => t.repeat)
|
||||||
|
|
||||||
return Promise.all(tasks)
|
return Promise.all(tasks)
|
||||||
}
|
}
|
||||||
@@ -97,9 +97,24 @@ const TS = new TaskManager()
|
|||||||
|
|
||||||
// create and clean recurrent events
|
// create and clean recurrent events
|
||||||
TS.add(new Task({
|
TS.add(new Task({
|
||||||
name: 'RECURRENT_EVENT',
|
name: 'CREATE_RECURRENT_EVENT',
|
||||||
method: eventController._createRecurrent,
|
method: eventController._createRecurrent,
|
||||||
repeatEach: 10 * minute // check each 10 minutes
|
repeatDelay: hour / 2 // check each half an hour
|
||||||
|
}))
|
||||||
|
|
||||||
|
// remove unrelated places
|
||||||
|
TS.add(new Task({
|
||||||
|
name: 'CLEAN_UNUSED_PLACES',
|
||||||
|
method: placeHelpers._cleanUnused,
|
||||||
|
repeatDelay: day,
|
||||||
|
callAtStart: true
|
||||||
|
}))
|
||||||
|
|
||||||
|
TS.add(new Task({
|
||||||
|
name: 'CLEAN_UNUSED_TAGS',
|
||||||
|
method: tagHelpers._cleanUnused,
|
||||||
|
repeatDelay: day,
|
||||||
|
callAtStart: true
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// daily morning notification
|
// daily morning notification
|
||||||
@@ -122,6 +137,7 @@ TS.add(new Task({
|
|||||||
// method: places._nominatimQuery,
|
// method: places._nominatimQuery,
|
||||||
// repeatEach: 60
|
// repeatEach: 60
|
||||||
// }))
|
// }))
|
||||||
|
//
|
||||||
|
|
||||||
// TS.start()
|
// TS.start()
|
||||||
// TS.add(new Task({ name: 'removable #1', method: daje, args: ['removable #1'], removable: true }))
|
// TS.add(new Task({ name: 'removable #1', method: daje, args: ['removable #1'], removable: true }))
|
||||||
|
|||||||
Reference in New Issue
Block a user