2021-08-27 14:32:44 +02:00
|
|
|
import Bluebird from 'bluebird'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { logger } from '../../helpers/logger.js'
|
2018-12-20 14:31:11 +01:00
|
|
|
|
2018-01-11 09:35:50 +01:00
|
|
|
export abstract class AbstractScheduler {
|
|
|
|
|
2018-06-14 18:06:56 +02:00
|
|
|
protected abstract schedulerIntervalMs: number
|
|
|
|
|
2023-10-11 09:20:23 +02:00
|
|
|
private interval: NodeJS.Timeout
|
2018-12-20 14:31:11 +01:00
|
|
|
private isRunning = false
|
2018-01-11 09:35:50 +01:00
|
|
|
|
|
|
|
enable () {
|
2018-06-14 18:06:56 +02:00
|
|
|
if (!this.schedulerIntervalMs) throw new Error('Interval is not correctly set.')
|
|
|
|
|
|
|
|
this.interval = setInterval(() => this.execute(), this.schedulerIntervalMs)
|
2018-01-11 09:35:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
disable () {
|
|
|
|
clearInterval(this.interval)
|
|
|
|
}
|
|
|
|
|
2018-12-20 14:31:11 +01:00
|
|
|
async execute () {
|
|
|
|
if (this.isRunning === true) return
|
|
|
|
this.isRunning = true
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.internalExecute()
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot execute %s scheduler.', this.constructor.name, { err })
|
|
|
|
} finally {
|
|
|
|
this.isRunning = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 15:38:53 +02:00
|
|
|
protected abstract internalExecute (): Promise<any> | Bluebird<any>
|
2018-01-11 09:35:50 +01:00
|
|
|
}
|