-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbase-updater.ts
35 lines (28 loc) · 960 Bytes
/
base-updater.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Logger, OnApplicationShutdown } from '@nestjs/common'
import { SchedulerRegistry } from '@nestjs/schedule'
export abstract class BaseUpdater implements OnApplicationShutdown {
private timeout?: NodeJS.Timeout
protected readonly logger = new Logger(this.constructor.name)
protected abstract interval: number
protected abstract scheduler: SchedulerRegistry
async start(): Promise<void> {
this.logger.debug(
`Running ${this.constructor.name} every ${this.interval} ms ...`,
)
await this.update()
this.timeout = setInterval(() => {
void this.update()
}, this.interval)
this.scheduler.addInterval(this.constructor.name, this.timeout)
}
stop(): void {
if (this.timeout) {
this.logger.debug(`Stopping updater ${this.constructor.name} ...`)
clearInterval(this.timeout)
}
}
abstract update(): Promise<void>
onApplicationShutdown(_signal?: string): void {
this.stop()
}
}