2020-02-13 16:06:52 +01:00
|
|
|
import { logger } from '../../helpers/logger'
|
2022-06-03 10:54:30 +02:00
|
|
|
import Bluebird from 'bluebird'
|
2020-02-14 14:09:31 +01:00
|
|
|
import { inspect } from 'util'
|
2020-02-13 16:06:52 +01:00
|
|
|
|
|
|
|
export abstract class AbstractScheduler {
|
|
|
|
|
|
|
|
protected abstract schedulerIntervalMs: number
|
|
|
|
|
|
|
|
private interval: NodeJS.Timer
|
|
|
|
private isRunning = false
|
|
|
|
|
|
|
|
enable () {
|
|
|
|
if (!this.schedulerIntervalMs) throw new Error('Interval is not correctly set.')
|
|
|
|
|
|
|
|
this.interval = setInterval(() => this.execute(), this.schedulerIntervalMs)
|
|
|
|
}
|
|
|
|
|
|
|
|
disable () {
|
|
|
|
clearInterval(this.interval)
|
|
|
|
}
|
|
|
|
|
|
|
|
async execute () {
|
2020-10-23 09:46:41 +02:00
|
|
|
if (this.isRunning === true) {
|
|
|
|
logger.info('Do not run scheduler %s because the process is already running.', this.constructor.name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-13 16:06:52 +01:00
|
|
|
this.isRunning = true
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.internalExecute()
|
|
|
|
} catch (err) {
|
2020-05-28 16:32:49 +02:00
|
|
|
logger.error({ err: inspect(err) }, 'Cannot execute %s scheduler.', this.constructor.name)
|
2020-02-13 16:06:52 +01:00
|
|
|
} finally {
|
|
|
|
this.isRunning = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract internalExecute (): Promise<any> | Bluebird<any>
|
|
|
|
}
|