]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import Bluebird from 'bluebird' |
2f5c6b2f C |
2 | import { logger } from '../../helpers/logger' |
3 | ||
60650c77 C |
4 | export 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 | } |