message-interceptor.ts 811 B

12345678910111213141516171819202122232425
  1. import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
  2. import { Observable } from 'rxjs';
  3. import { finalize, tap } from 'rxjs/operators';
  4. import { WorkerMonitor } from './worker-monitor';
  5. /**
  6. * This interceptor is used to keep track of open worker tasks, so that the WorkerModule
  7. * is not allowed to be destroyed while tasks are in progress.
  8. */
  9. @Injectable()
  10. export class MessageInterceptor implements NestInterceptor {
  11. constructor(private monitor: WorkerMonitor) {}
  12. intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
  13. this.monitor.increment();
  14. return next
  15. .handle()
  16. .pipe(
  17. finalize(() => {
  18. this.monitor.decrement();
  19. }),
  20. );
  21. }
  22. }