/
app.service.ts
32 lines (28 loc) · 1.07 KB
/
app.service.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
import { Injectable, Logger } from '@nestjs/common';
import { Worker, isMainThread } from 'worker_threads';
import workerThreadFilePath from './worker-threads/config';
@Injectable()
export class AppService {
private readonly logger = new Logger(AppService.name);
checkMainThread() {
this.logger.debug(
'Are we on the main thread here?',
isMainThread ? 'Yes.' : 'No.',
);
}
// do not run this from the worker thread or you will spawn an infinite number of threads in cascade
runWorker(fibonacci: number): string {
this.checkMainThread();
// eslint-disable-next-line @typescript-eslint/no-this-alias
const thisService = this;
const worker = new Worker(workerThreadFilePath, {
workerData: fibonacci,
});
worker.on('message', (fibonacciSum) => {
thisService.logger.verbose('Calculated sum', fibonacciSum);
});
worker.on('error', (e) => console.log('on error', e));
worker.on('exit', (code) => console.log('on exit', code));
return 'Processing the fibonacci sum... Check NestJS app console for the result.';
}
}