1.0.4 • Published 1 year ago

nestjs-temporalio v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Description

NestJs Module for Temporal that support multiple workers.

Installation

yarn add nestjs-temporalio

Quick start

// main.ts

const id = 'TestWorker';

async function bootstrap() {
  // You can also createApplicationContext.
  const app = await NestFactory.create(AppModule);

  // wait runtime bundle workflow
  await new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, 1000);
  });

  await app.listen(3000);

  // You can create multiple
  const workers = [
    // id is equal to identify / workerId
    app.get(`Worker_${id}`),
  ];

  // You may need to shutdown the worker using OnModuleDestroy hooks.
  await Promise.all(workers.map((worker) => worker.run()));
}
bootstrap();
// test-service.interface.ts
export interface ITestService {
  greet(): Promise<string>;
}
// test.service.ts
@Injectable()
export class TestService implements ITestService {
  async greet(): Promise<string> {
    return Promise.resolve('Hello');
  }
}
// test.workflow.ts
const { greet } = proxyActivities<ITestService>({
  startToCloseTimeout: '10 seconds',
});

export const testWorkflow = async (): Promise<string> => {
  return greet();
};
// app.module.ts
@Module({
  imports: [
    TemporalModule.forRootAsync({
      useFactory: async () => {
        const connection = await NativeConnection.connect();

        const workflowBundle = await bundleWorkflowCode({
          workflowsPath: path.join(__dirname, './workflows'),
        });

        return {
          workflowBundle,
          taskQueue: 'QueueName',
          connection,
          activities: [TestService],
        };
      },
      workerId,
    }),
  ],
  providers: [TestService],
})
export class AppModule {}