gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:01:36 32 4
gpt4 key购买 nike

我使用 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();
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);
}, 1000);
}

}

最佳答案

我会说你应该实现 OnModuleInit界面。 Read more about lifecycle hooks .

关于nestjs - 在引导运行后运行组件(服务)方法的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48719984/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com