小编bre*_*ley的帖子

在引导程序运行后运行组件(服务)方法的最佳方法是什么?

我使用 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();
Run Code Online (Sandbox Code Playgroud)

src/app.service.ts

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);
        }, …
Run Code Online (Sandbox Code Playgroud)

nestjs

3
推荐指数
1
解决办法
932
查看次数

标签 统计

nestjs ×1