KurtzL / nestjs-temporal

Temporal plugin for nestjs framework
MIT License
102 stars 21 forks source link

Dependency injection for activities #29

Open yossi-mycedar opened 1 year ago

yossi-mycedar commented 1 year ago

Hey. First of all thanks a lot for this module. It seems like there is an issue when trying to inject / import other services to this module in order to be able to use them in activities (Activity should be able to use a service -> which has a repository injected to it -> etc...) Any suggestion? Thanks

adzamkomladev commented 1 year ago

Hi, @yossi-mycedar Do you have a sample?

shaolinjr commented 1 year ago

When your activity needs to inject other services, how can it be done?

adzamkomladev commented 1 year ago

The same way you inject dependencies into your services (normal nestjs way). @shaolinjr

yossi-mycedar commented 1 year ago

@adzamkomladev Sorry for the late response. Currently I'm doing it in the following way: (the following code was taken from app.module.ts)

// Configure a Temporal worker
    TemporalModule.forRootAsync({
      imports: [ClientsModule, ConfigModule],
      inject: ['ClientsService', ConfigService],
      useFactory: async (
        clientsService: ClientsService,
        configService: ConfigService
      ) => {
        Runtime.install({});

        const clientCert = fs.readFileSync(
          `./${configService.get<string>('TEMPORAL_CERT_FILENAME')}.pem`
        );
        const clientKey = fs.readFileSync(
          `./${configService.get<string>('TEMPORAL_CERT_FILENAME')}.key`
        );

        const connection = await NativeConnection.connect({
          address: configService.get<string>('TEMPORAL_HOST'), // defaults port to 7233 if not specified
          tls: {
            clientCertPair: {
              crt: clientCert,
              key: clientKey,
            },
          },
        });

        const workflowBundle = await bundleWorkflowCode({
          workflowsPath: require.resolve('./clients/temporal/workflows'),
        });

        return {
          connection,
          activities: clientActivities(clientsService),
          taskQueue: TEMPORAL_QUEUE_NAME,
          namespace: configService.get<string>('TEMPORAL_NAMESPACE'),
          workflowBundle,
        };
      },
    }),

Have a look at:

activities: clientActivities(clientsService),

But it feels to me like a workaround (cause defining my service in app.module sounds wrong to me).

Any suggestion / thoughts? Thanks a lot.