skip to Main Content

I have create a microService for shearing Proto files (server). and i have module and service for calling this.

now in my (client), i want to call that microservice as soon as my application started. I already use onApplicationBootstrap() in my app.module file. but thats not work, because I use onModule init for my grpc client config in other modules. is there any way to run this, befor any other modules?(in need to call my CallProtoService onetime befor any module)

this is my module code

import { Module, OnModuleInit } from "@nestjs/common";
import { GrpcProtoModule } from "../grpcProto/grpcProto.module";
import { GrpcProtoService } from "../grpcProto/grpcProto.service";

@Module({
    imports: [GrpcProtoModule],
    // providers: [CallProtoService],
    // exports: [CallProtoService],
})
   export class CallProtoModule implements OnModuleInit {
    constructor(private readonly grpcProtoService: GrpcProtoService) { }
    
    async onModuleInit() {
        const markupTrainGrpcProto = {
            packageName: 'markupTrainGrpc',
        };
        const gensecgrpcProto = {
            packageName: 'gensecgrpc',
        };

        await this.grpcProtoService.getProto(gensecgrpcProto);
        await this.grpcProtoService.getProto(markupTrainGrpcProto);
    }


}

2

Answers


  1. Chosen as BEST ANSWER

    at last I find a solution. I add this code in my main.ts file.

    async function bootstrap() {
    //this line will CallProtoModule before AppModule created.
      await NestFactory.createApplicationContext(CallProtoModule);
      const app = await NestFactory.create(AppModule, {
        logger: WinstonModule.createLogger({
          instance: instance,
        }),
      });
    

    hope this helpful for others.


  2. You wrote that you want to call some NestJS service before anything else starts. What’s the reason for doing so? Bootstrapping is for, well, bootstrapping, which means that no actual business logic should be handled on the bootstrapping phase, without guarantee that it will work correctly. Think of some time-consuming operations that would be started before application knows that it can start (it determines if it can start on bootstrap -> bindings to the IoC are attempted to be resolved, and if something is buggy, application won’t start). If there’s a time-consuming operation on the bootstrap, then it can be forcibly stopped before final execution, which is problematic.

    That’s exactly the reason why any module or service provider au general must never fetch any config for bootstrapping from databases, because databases are managed by other modules, that may or may not be fully resolved and operative.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search