]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/schedulers/abstract-scheduler.ts
Merge branch 'release/v1.2.0'
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / abstract-scheduler.ts
CommitLineData
2f5c6b2f
C
1import { logger } from '../../helpers/logger'
2
60650c77
C
3export abstract class AbstractScheduler {
4
2baea0c7
C
5 protected abstract schedulerIntervalMs: number
6
60650c77 7 private interval: NodeJS.Timer
2f5c6b2f 8 private isRunning = false
60650c77
C
9
10 enable () {
2baea0c7
C
11 if (!this.schedulerIntervalMs) throw new Error('Interval is not correctly set.')
12
13 this.interval = setInterval(() => this.execute(), this.schedulerIntervalMs)
60650c77
C
14 }
15
16 disable () {
17 clearInterval(this.interval)
18 }
19
2f5c6b2f
C
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>
60650c77 34}