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