Nestjs 什么';在引导运行之后运行组件(服务)方法的最佳方式是什么?

Nestjs 什么';在引导运行之后运行组件(服务)方法的最佳方式是什么?,nestjs,Nestjs,我使用NestJS实例作为微服务(没有HTTP) 我需要在引导初始化之后使用一些业务逻辑运行组件的无限循环方法 最好的方法是什么 src/main.ts import {NestFactory} from '@nestjs/core'; import {ApplicationModule} from './app.module'; import {Transport} from '@nestjs/microservices'; async function bootstrap() {

我使用NestJS实例作为微服务(没有HTTP)

我需要在引导初始化之后使用一些业务逻辑运行组件的无限循环方法

最好的方法是什么

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();
import { Component } from '@nestjs/common';

@Component()
export class AppService {

    startLoop() {
        let timerId = setTimeout(function loop() {
            console.log('Loop process');
            // Some business logic here
            timerId = setTimeout(loop, 1000);
        }, 1000);
    }

}
src/app.service.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();
import { Component } from '@nestjs/common';

@Component()
export class AppService {

    startLoop() {
        let timerId = setTimeout(function loop() {
            console.log('Loop process');
            // Some business logic here
            timerId = setTimeout(loop, 1000);
        }, 1000);
    }

}

我认为您应该实现
OnModuleInit
接口,阅读更多关于生命周期挂钩的信息。

是的,我考虑过。但我不确定这样做是否正确。谢谢更新链接: