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

feat(temporal): add new module for asynchronous workflows with temporal.io #2464

Merged
merged 8 commits into from
Oct 13, 2023
Merged
7 changes: 6 additions & 1 deletion docs/.vuepress/config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ module.exports = ({title, description, base = "", url, apiRedirectUrl = "", them
text: "Serverless",
link: `${base}/tutorials/serverless.html`
},
{
text: "Temporal",
Romakita marked this conversation as resolved.
Show resolved Hide resolved
link: `${base}/tutorials/temporal.html`
},
{
text: "Terminus",
link: `${base}/tutorials/terminus.html`
Expand Down Expand Up @@ -419,7 +423,8 @@ module.exports = ({title, description, base = "", url, apiRedirectUrl = "", them
{title: "Serverless", path: base + "/tutorials/serverless"},
{title: "IORedis", path: base + "/tutorials/ioredis"},
{title: "Objection.js", path: base + "/tutorials/objection"},
{title: "Vite plugin ssr", path: base + "/tutorials/vite-plugin-ssr"}
{title: "Vite plugin ssr", path: base + "/tutorials/vite-plugin-ssr"},
{title: "Temporal", path: base + "/tutorials/temporal"}
].sort((a, b) => (a.title < b.title ? -1 : 1))
},
{
Expand Down
11 changes: 11 additions & 0 deletions docs/.vuepress/public/temporal.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,15 @@ frameworks:
- title: Agenda
href: /tutorials/agenda.html
src: /agenda.svg
- title: Serveless
- title: Serverless
href: /tutorials/serverless.html
src: /serverless.svg
- title: Terminus
href: /tutorials/terminus.html
src: /package.svg
- title: Temporal
href: /tutorials/temporal.html
src: /temporal.svg
---

::: slot hero-brand
Expand Down
221 changes: 221 additions & 0 deletions docs/tutorials/temporal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
---
meta:
- name: description
content: Use Temporal.io with Express/Koa, TypeScript and Ts.ED. Temporal is an open source durable execution system. Write code that’s fault tolerant, durable, and simple.
- name: keywords
content: ts.ed express typescript temporal temporal.io node.js javascript decorators
---

# Temporal

<Banner src="/temporal.svg" href="https://temporal.io/" height="38" />

## Feature

[Temporal](https://temporal.io) lets you write complex asynchronous distributed workflows using easy to read linear code. For more information about Temporal take a look at the documentation [here](https://docs.temporal.io/).

The `@tsed/temporal` module allows you to decorate classes with `@Temporal` and
corresponding methods with `@Activity` to write and start asynchronous workflows.

Inject the `TemporalClient` to start/schedule and query workflows or to send signals to them.

Use the `bootstrapWorker` helper to start a queue that executes your workflows and activities.

## Installation

To begin, install the Temporal module for Ts.ED:

```bash
npm install --save @tsed/temporal
npm install --save @temporalio/client @temporalio/worker
```

## Configure your server

Import `@tsed/temporal` in your Server:

```typescript
import {Configuration} from "@tsed/common";
import "@tsed/temporal"; // import temporal ts.ed module

@Configuration({
temporal: {
enabled: true,
connection: {
/* optional: see ConnectionOptions of @temporalio/client */
},
client: {
/* optional: see ClientOptions of @temporalio/client */
}
}
})
export class Server {}
```

## Create a new Service

Decorate the class with `@Temporal`.

Use the `@Activity` decorator to define activities.

```typescript
import {Temporal, Activity} from "@tsed/agenda";

@Temporal()
export class UserOnboardingActivities {
constructor(private userService: UserService, private emailService: EmailService) {}

@Activity()
async sendVerificationEmail(email: string) {
return this.emailService.sendVerificationEmail(email);
}

@Activity()
async activateUser(email: string) {
return this.userService.activateUser(email);
}

@Activity()
async sendWelcomeEmail(email: string) {
return this.emailService.sendWelcomeEmail(email);
}

@Activity()
async sendFollowUpEmail(email: string) {
return this.emailService.sendFollowUpEmail(email);
}

@Activity()
async deleteUser(email: string) {
return this.userService.deleteUser(email);
}
}
```

Optional, create an interface for your activities to use it later for your [workflows](https://docs.temporal.io/workflows).

```ts
interface IUserOnboardingActivities {
sendVerificationEmail(email: string): Promise<void>;
activateUser(email: string): Promise<void>;
sendWelcomeEmail(email: string): Promise<void>;
sendFollowUpEmail(email: string): Promise<void>;
deleteUser(email: string): Promise<void>;
}

export type Activities = IGreetingActivity;
```

### Write Workflows

Workflows are regular functions that cannot interact directly with Ts.ED or other packages. Just the earlier created interface is used for type-safety.

::: warning
Do not import any non temporal packages here. The workflows are bundled internally by the worker and won't have access to anything else.
:::

```ts
import {proxyActivities, defineSignal, setHandler, condition, sleep} from "@temporalio/workflow";
import {Activities} from "../activities";

export const isVerifiedSignal = defineSignal("verificationSignal");

export async function onboardUser(email: string): Promise<string> {
const {sendVerificationEmail, activateUser, sendWelcomeEmail, sendFollowUpEmail, deleteUser} = proxyActivities<Activities>({
startToCloseTimeout: "1 minute"
});

let isVerified = false;
setHandler(isVerifiedSignal, () => {
isVerified = true;
});

// 1. Send verification email
await sendVerificationEmail(email);

// 2. Wait for verification ...
const verifiedInTime = await condition(() => isVerified, "1w" /* or for a timeout */);
if (!verifiedInTime) {
// 3a. If not verified in time, delete user
await deleteUserAndTenant(email);
return false;
}

// 3b. If verified in time, send welcome email
await sendWelcomeEmail(email);

// 4. Send follow up email after one day
await sleep("1d"); // special sleep function by temporal
await sendFollowUpEmail(email);
}
```

## Inject TemporalClient

Inject the TemporalClient instance to interact with it directly, e.g. to start a workflow.

```typescript
import {Service, AfterRoutesInit} from "@tsed/common";
import {TemporalClient} from "@tsed/temporal";
import {onboardUser} from "../workflows";

@Service()
export class UsersService implements AfterRoutesInit {
@Inject()
private temporalClient: TemporalClient;

async create(user: User): Promise<User> {
// ...
await this.temporalClient.workflow.start(onboardUser, {
args: [user.email],
taskQueue: "onboarding",
workflowId: `onboarding-${user.id}`
});
}
}
```

### Start a worker

The workflows and activities won't get executed until you start a worker. This module provides a helper function to bootstrap a worker based on your Ts.ED server class that is aware of all your activities.

The most tricky part is the `workflowsPath` parameter. This is the path of the file/folder where the workflows are exported. The file is automatically loaded when the worker is started and internally bundled with webpack. The path is highly dependent on your project structure and build process.

Read more about it [here](https://docs.temporal.io/typescript/troubleshooting/#webpack-errors).

```ts
import {bootstrapWorker} from "@tsed/temporal";
import {Server} from "./app/Server";

const worker = await bootstrapWorker(Server, {
worker: {
taskQueue: "onboarding",
workflowsPath: require.resolve("./temporal") // other example: path.join(process.cwd(), 'dist/apps/api/temporal/index.ts');
},
connection: {
/* optional: see NativeConnectionOptions of @temporalio/worker */
},
platform: {
/* optional: see PlatformBuilderSettings of @tsed/common */
componentsScan: false,
logger: {
level: "info"
}
}
});
await worker.run();
```

## Authors

<GithubContributors :users="['ochrstn']"/>

## Maintainers

<GithubContributors :users="['ochrstn']"/>

<div class="flex items-center justify-center p-5">
<Button href="/contributing.html" class="rounded-medium">
Become maintainer
</Button>
</div>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"test:graphql": "lerna run test --scope \"@tsed/{apollo,typegraphql}\" --stream",
"test:security": "lerna run test --scope \"@tsed/{jwks,oidc-provider,passport}\" --stream",
"test:specs": "lerna run test --scope \"@tsed/{ajv,exceptions,json-mapper,schema,swagger}\" --stream --concurrency 2",
"test:third-parties": "lerna run test --scope \"@tsed/{agenda,async-hook-context,components-scan,event-emitter,seq,socketio,stripe,terminus,vite-ssr-plugin}\" --stream --concurrency 4",
"test:third-parties": "lerna run test --scope \"@tsed/{agenda,async-hook-context,components-scan,event-emitter,seq,socketio,stripe,temporal,terminus,vite-ssr-plugin}\" --stream --concurrency 4",
"test:formio": "lerna run test --scope \"@tsed/{schema-formio,formio}\" --stream",
"coverage": "merge-istanbul --out coverage/coverage-final.json \"**/packages/**/coverage/coverage-final.json\" && nyc report --reporter text --reporter html --reporter lcov -t coverage --report-dir coverage",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
Expand Down
13 changes: 13 additions & 0 deletions packages/third-parties/temporal/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules
docs
docs-references
lib
dist
coverage
.nyc_output
*-lock.json
*.lock
benchmarks.*
**/generated

**/*.js
1 change: 1 addition & 0 deletions packages/third-parties/temporal/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@tsed/eslint/node.js");
19 changes: 19 additions & 0 deletions packages/third-parties/temporal/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html

module.exports = {
...require("@tsed/jest-config"),
roots: ["<rootDir>/src", "<rootDir>/test"],
coverageThreshold: {
global: {
statements: 94.62,
branches: 75.75,
functions: 93.33,
lines: 94.62
}
},
transformIgnorePatterns: ["test/workflows/.*\\.ts$"],
moduleNameMapper: {
"@tsed/temporal": "<rootDir>/src/index.ts"
}
};
47 changes: 47 additions & 0 deletions packages/third-parties/temporal/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@tsed/temporal",
"version": "7.36.8",
"description": "Temporal.io package for Ts.ED framework",
"source": "./src/index.ts",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"typings": "./lib/types/index.d.ts",
"exports": {
"types": "./lib/types/index.d.ts",
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js",
"default": "./lib/esm/index.js"
},
"scripts": {
"build": "yarn barrels && yarn build:ts",
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
"test": "cross-env NODE_ENV=test yarn jest --runInBand --forceExit && jest-coverage-thresholds-bumper ",
"build:ts": "tsc --build tsconfig.json && tsc --build tsconfig.esm.json",
"lint": "eslint '**/*.{ts,js}'",
"lint:fix": "eslint '**/*.{ts,js}' --fix"
},
"contributors": [
{
"name": "Oliver Christen"
}
],
"dependencies": {
"tslib": "2.5.0"
},
"private": false,
"devDependencies": {
"@temporalio/client": "^1.8.4",
"@temporalio/testing": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@tsed/common": "7.36.8",
"@tsed/core": "7.36.8",
"@tsed/di": "7.36.8",
"@tsed/eslint": "7.36.8",
"@tsed/typescript": "7.36.8",
"eslint": "^8.12.0"
},
"peerDependencies": {
"@temporalio/client": "^1.8.4",
"@temporalio/worker": "^1.8.4"
}
}
Loading
Loading