-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.worker.ts
39 lines (33 loc) · 957 Bytes
/
app.worker.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
36
37
38
39
import {
BeforeApplicationShutdown,
Injectable,
Logger,
LoggerService,
ShutdownSignal,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
@Injectable()
export class AppWorker implements BeforeApplicationShutdown {
private readonly logger: LoggerService = new Logger(AppWorker.name);
private isRunning = false;
private tickMs: number;
constructor(
private readonly configService: ConfigService<{
worker: { tickMs: number };
}>,
) {
this.tickMs = this.configService.get('worker.tickMs', { infer: true });
}
async run(): Promise<void> {
this.logger.log('running');
this.isRunning = true;
while (this.isRunning) {
await sleep(this.tickMs);
}
}
beforeApplicationShutdown(signal?: ShutdownSignal): any {
this.logger.log(`shutting down with signal ${signal}`);
this.isRunning = false;
}
}