Giter VIP home page Giter VIP logo

nestjs-temporal's Introduction

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

nestjs-temporal

Temporal module for Nest.

Quick start

import { Module } from '@nestjs/common';
import { TemporalModule } from 'nestjs-temporal';

@Module({
  imports: [
    TemporalModule.registerWorker({
      workerOptions: {
        taskQueue: 'default',
        workflowsPath: require.resolve('./temporal/workflow'),
      },
    }),

    TemporalModule.registerClient(),
  ],
})
export class AppModule {
}
import { Injectable } from '@nestjs/common';
import { Activities, Activity } from 'nestjs-temporal';

@Injectable()
@Activities()
export class GreetingActivity {
  constructor() {}

  @Activity()
  async greeting(name: string): Promise<string> {
    return 'Hello ' + name;
  }
}

export interface IGreetingActivity {
  greeting(name: string): Promise<string>;
}
import { proxyActivities } from '@temporalio/workflow';
// Only import the activity types
import { IGreetingActivity } from '../activities';

const { greeting } = proxyActivities<IGreetingActivity>({
  startToCloseTimeout: '1 minute',
});

export async function example(name: string): Promise<string> {
  return await greeting(name);
}
import { Controller, Post } from '@nestjs/common';
import { Connection, WorkflowClient } from '@temporalio/client';
import { InjectTemporalClient } from 'nestjs-temporal';

@Controller()
export class AppController {
  constructor(
    @InjectTemporalClient() private readonly temporalClient: WorkflowClient,
  ) {}

  @Post()
  async greeting() {
    const handle = await this.temporalClient.start('example', {
      args: ['Temporal'],
      taskQueue: 'default',
      workflowId: 'wf-id-' + Math.floor(Math.random() * 1000),
    });
    console.log(`Started workflow ${handle.workflowId}`);
  }
}

Advanced Options

  • Creating the Worker connection:
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TemporalModule } from 'nestjs-temporal';
import { bundleWorkflowCode, NativeConnection, Runtime } from '@temporalio/worker';
import * as path from 'path';

@Module({
  imports: [
    TemporalModule.registerWorkerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => {
        Runtime.install({});
        const temporalHost = config.get('app.temporalHost');
        const connection = await NativeConnection.connect({
          address: temporalHost,
        });
        const workflowBundle = await bundleWorkflowCode({
          workflowsPath: path.join(__dirname, './workflows'),
        });

        return {
          workerOptions: {
            connection,
            taskQueue: 'default',
            workflowBundle,
          }
        };
      },
    }),
    ClientModule,
  ],
})
export class AppModule {}
  • Creating the client connection:
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TemporalModule } from 'nestjs-temporal';
import { Connection } from '@temporalio/client';

@Module({
  imports: [
    TempModule.registerClientAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => {
        const temporalHost = config.get('app.temporalHost');
        const connection = await Connection.connect({
          address: temporalHost,
        });

        return {
          connection,
        };
      },
    }),
  ],
})
export class ClientModule {}

Multiple workers

Multiple workers can be registered by making multiple calls to TemporalModule.registerWorker or TemporalModule.registerWorkerAsync.

You must explicitly specify your activity classes when registering multiple workers; otherwise, the workers may register all activities marked by @Activity(). You may find it more convenient to import dynamic worker module into the module that actually contains the workflow and activities rather than your root AppModule.

// Register the client once at the app level
import { Module } from '@nestjs/common';
import { TemporalModule } from 'nestjs-temporal';

@Module({
  imports: [
    TemporalModule.registerClient(),
  ],
})
export class AppModule {
}

// Configure Worker #1
@Module({
  imports: [
    TemporalModule.registerWorker({
      workerOptions: {
        taskQueue: 'worker-1',
        workflowsPath: require.resolve('./temporal/workflow-1'),
      },
      activityClasses: [Greeting1Activity],
    }),
  ],
  providers: [Greeting1Activity],
})
export class Worker1Module {
}

// Configure Worker #2
@Module({
  imports: [
    TemporalModule.registerWorker({
      workerOptions: {
        taskQueue: 'worker-2',
        workflowsPath: require.resolve('./temporal/workflow-2'),
      },
      activityClasses: [SomeOtherActivity],
    }),
  ],
  providers: [SomeOtherActivity],
})
export class Worker2Module {
}

People

License

Nest is MIT licensed.

nestjs-temporal's People

Contributors

kurtzl avatar clintonb avatar dependabot[bot] avatar jdnichollsc avatar prashant-surya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.