我使用NestJS实例作为微服务(没有HTTP)。
我需要在引导初始化之后运行Component的无限循环方法和一些业务逻辑。
最好的方法是什么?
的src / main.ts
import {NestFactory} from '@nestjs/core';
import {ApplicationModule} from './app.module';
import {Transport} from '@nestjs/microservices';
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
app.connectMicroservice({
transport: Transport.REDIS,
url: 'redis://:redis_pass@localhost:6379',
});
await app.startAllMicroservicesAsync();
// Probably here I must run startLoop method from app.service.ts
}
bootstrap();
的src / app.service.ts
import { Component } from '@nestjs/common';
@Component()
export class AppService {
startLoop() {
let timerId = setTimeout(function loop() {
console.log('Loop process');
// Some bussines logic here
timerId = setTimeout(loop, 1000);
}, 1000);
}
}