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