You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
main
${ noResults }
36 lines
841 B
TypeScript
36 lines
841 B
TypeScript
import Bluebird from 'bluebird'
|
|||
import { logger } from '../../helpers/logger.js'
|
|||
|
|||
export abstract class AbstractScheduler {
|
|||
|
|||
protected abstract schedulerIntervalMs: number
|
|||
|
|||
private interval: NodeJS.Timeout
|
|||
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 () {
|
|||
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
|
|||
}
|
|||
}
|
|||
|
|||
protected abstract internalExecute (): Promise<any> | Bluebird<any>
|
|||
}
|