]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/schedulers/abstract-scheduler.ts
Merge branch 'develop' into pr/1217
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / abstract-scheduler.ts
1 import { logger } from '../../helpers/logger'
2
3 export abstract class AbstractScheduler {
4
5 protected abstract schedulerIntervalMs: number
6
7 private interval: NodeJS.Timer
8 private isRunning = false
9
10 enable () {
11 if (!this.schedulerIntervalMs) throw new Error('Interval is not correctly set.')
12
13 this.interval = setInterval(() => this.execute(), this.schedulerIntervalMs)
14 }
15
16 disable () {
17 clearInterval(this.interval)
18 }
19
20 async execute () {
21 if (this.isRunning === true) return
22 this.isRunning = true
23
24 try {
25 await this.internalExecute()
26 } catch (err) {
27 logger.error('Cannot execute %s scheduler.', this.constructor.name, { err })
28 } finally {
29 this.isRunning = false
30 }
31 }
32
33 protected abstract internalExecute (): Promise<any>
34 }