1
0
Fork 0
peertube/server/lib/schedulers/abstract-scheduler.ts
2018-12-20 14:31:11 +01:00

34 lines
788 B
TypeScript

import { logger } from '../../helpers/logger'
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 () {
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>
}