Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency injection for activities #29

Open
yossi-mycedar opened this issue Nov 23, 2022 · 4 comments
Open

Dependency injection for activities #29

yossi-mycedar opened this issue Nov 23, 2022 · 4 comments

Comments

@yossi-mycedar
Copy link

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
Copy link

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

@shaolinjr
Copy link

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

@adzamkomladev
Copy link

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

@yossi-mycedar
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants