@tsed/temporal 7.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +13 -0
- package/.eslintrc.js +1 -0
- package/jest.config.js +19 -0
- package/lib/cjs/TemporalModule.js +53 -0
- package/lib/cjs/TemporalModule.js.map +1 -0
- package/lib/cjs/constants.js +5 -0
- package/lib/cjs/constants.js.map +1 -0
- package/lib/cjs/decorators/activity.js +17 -0
- package/lib/cjs/decorators/activity.js.map +1 -0
- package/lib/cjs/decorators/temporal.js +13 -0
- package/lib/cjs/decorators/temporal.js.map +1 -0
- package/lib/cjs/index.js +15 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/interfaces/TemporalStore.js +5 -0
- package/lib/cjs/interfaces/TemporalStore.js.map +1 -0
- package/lib/cjs/interfaces/interfaces.js +3 -0
- package/lib/cjs/interfaces/interfaces.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/services/TemporalFactory.js +51 -0
- package/lib/cjs/services/TemporalFactory.js.map +1 -0
- package/lib/cjs/utils/worker.js +35 -0
- package/lib/cjs/utils/worker.js.map +1 -0
- package/lib/esm/TemporalModule.js +50 -0
- package/lib/esm/TemporalModule.js.map +1 -0
- package/lib/esm/constants.js +2 -0
- package/lib/esm/constants.js.map +1 -0
- package/lib/esm/decorators/activity.js +13 -0
- package/lib/esm/decorators/activity.js.map +1 -0
- package/lib/esm/decorators/temporal.js +9 -0
- package/lib/esm/decorators/temporal.js.map +1 -0
- package/lib/esm/index.js +12 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/TemporalStore.js +2 -0
- package/lib/esm/interfaces/TemporalStore.js.map +1 -0
- package/lib/esm/interfaces/interfaces.js +2 -0
- package/lib/esm/interfaces/interfaces.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/services/TemporalFactory.js +48 -0
- package/lib/esm/services/TemporalFactory.js.map +1 -0
- package/lib/esm/utils/worker.js +31 -0
- package/lib/esm/utils/worker.js.map +1 -0
- package/lib/tsconfig.esm.tsbuildinfo +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types/TemporalModule.d.ts +12 -0
- package/lib/types/constants.d.ts +1 -0
- package/lib/types/decorators/activity.d.ts +2 -0
- package/lib/types/decorators/temporal.d.ts +1 -0
- package/lib/types/index.d.ts +11 -0
- package/lib/types/interfaces/TemporalStore.d.ts +9 -0
- package/lib/types/interfaces/interfaces.d.ts +12 -0
- package/lib/types/services/TemporalFactory.d.ts +5 -0
- package/lib/types/utils/worker.d.ts +9 -0
- package/package.json +49 -0
- package/readme.md +256 -0
- package/src/TemporalModule.ts +39 -0
- package/src/constants.ts +1 -0
- package/src/decorators/activity.spec.ts +40 -0
- package/src/decorators/activity.ts +14 -0
- package/src/decorators/temporal.spec.ts +11 -0
- package/src/decorators/temporal.ts +11 -0
- package/src/index.ts +12 -0
- package/src/interfaces/TemporalStore.ts +9 -0
- package/src/interfaces/interfaces.ts +14 -0
- package/src/services/TemporalFactory.ts +55 -0
- package/src/utils/worker.ts +43 -0
- package/tsconfig.esm.json +39 -0
- package/tsconfig.json +39 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Logger } from "@tsed/common";
|
|
2
|
+
import { InjectorService } from "@tsed/di";
|
|
3
|
+
import { TemporalClient } from "./services/TemporalFactory";
|
|
4
|
+
export declare class TemporalModule {
|
|
5
|
+
private temporalServices;
|
|
6
|
+
protected logger: Logger;
|
|
7
|
+
protected injector: InjectorService;
|
|
8
|
+
protected client: TemporalClient;
|
|
9
|
+
constructor(temporalServices: any[]);
|
|
10
|
+
getActivities(): object;
|
|
11
|
+
protected getActivitiesFromInstance(instance: any): {};
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const PROVIDER_TYPE_TEMPORAL = "PROVIDER_TYPE_TEMPORAL";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function Temporal(): ClassDecorator;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by barrelsby.
|
|
3
|
+
*/
|
|
4
|
+
export * from "./TemporalModule";
|
|
5
|
+
export * from "./constants";
|
|
6
|
+
export * from "./decorators/activity";
|
|
7
|
+
export * from "./decorators/temporal";
|
|
8
|
+
export * from "./interfaces/TemporalStore";
|
|
9
|
+
export * from "./interfaces/interfaces";
|
|
10
|
+
export * from "./services/TemporalFactory";
|
|
11
|
+
export * from "./utils/worker";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ClientOptions, ConnectionOptions } from "@temporalio/client";
|
|
2
|
+
declare global {
|
|
3
|
+
namespace TsED {
|
|
4
|
+
interface Configuration {
|
|
5
|
+
temporal?: {
|
|
6
|
+
enabled?: boolean;
|
|
7
|
+
connection?: ConnectionOptions;
|
|
8
|
+
client?: Omit<ClientOptions, "connection">;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PlatformBuilderSettings } from "@tsed/common";
|
|
2
|
+
import { NativeConnection, NativeConnectionOptions, Worker, WorkerOptions } from "@temporalio/worker";
|
|
3
|
+
type BootstrapWorkerOptions = {
|
|
4
|
+
platform?: PlatformBuilderSettings;
|
|
5
|
+
worker: Omit<WorkerOptions, "activities">;
|
|
6
|
+
connection?: NativeConnection | NativeConnectionOptions;
|
|
7
|
+
};
|
|
8
|
+
export declare function bootstrapWorker(mod: any, settings: BootstrapWorkerOptions): Promise<Worker>;
|
|
9
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsed/temporal",
|
|
3
|
+
"version": "7.37.0",
|
|
4
|
+
"description": "Temporal.io package for Ts.ED framework",
|
|
5
|
+
"source": "./src/index.ts",
|
|
6
|
+
"main": "./lib/cjs/index.js",
|
|
7
|
+
"module": "./lib/esm/index.js",
|
|
8
|
+
"typings": "./lib/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"import": "./lib/esm/index.js",
|
|
12
|
+
"require": "./lib/cjs/index.js",
|
|
13
|
+
"default": "./lib/esm/index.js"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "yarn barrels && yarn build:ts",
|
|
17
|
+
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
|
|
18
|
+
"test": "cross-env NODE_ENV=test yarn jest --runInBand --forceExit && jest-coverage-thresholds-bumper ",
|
|
19
|
+
"build:ts": "tsc --build tsconfig.json && tsc --build tsconfig.esm.json",
|
|
20
|
+
"lint": "eslint '**/*.{ts,js}'",
|
|
21
|
+
"lint:fix": "eslint '**/*.{ts,js}' --fix"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"tslib": "2.5.0"
|
|
25
|
+
},
|
|
26
|
+
"private": false,
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@temporalio/client": "^1.8.4",
|
|
29
|
+
"@temporalio/testing": "^1.8.4",
|
|
30
|
+
"@temporalio/worker": "^1.8.4",
|
|
31
|
+
"@tsed/common": "7.37.0",
|
|
32
|
+
"@tsed/core": "7.37.0",
|
|
33
|
+
"@tsed/di": "7.37.0",
|
|
34
|
+
"@tsed/eslint": "7.37.0",
|
|
35
|
+
"@tsed/typescript": "7.37.0",
|
|
36
|
+
"eslint": "^8.12.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@temporalio/client": "^1.8.4",
|
|
40
|
+
"@temporalio/worker": "^1.8.4"
|
|
41
|
+
},
|
|
42
|
+
"repository": "https://github.com/tsedio/tsed",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/tsedio/tsed/issues"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/tsedio/tsed/tree/production/packages/third-parties/temporal",
|
|
47
|
+
"author": "Romain Lenzotti",
|
|
48
|
+
"license": "MIT"
|
|
49
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
<p style="text-align: center" align="center">
|
|
2
|
+
<a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
|
|
7
|
+
<h1>Temporal</h1>
|
|
8
|
+
|
|
9
|
+
[](https://github.com/tsedio/tsed/actions?query=workflow%3A%22Build+%26+Release%22)
|
|
10
|
+
[](https://github.com/tsedio/tsed/blob/master/CONTRIBUTING.md)
|
|
11
|
+
[](https://coveralls.io/github/tsedio/tsed?branch=production)
|
|
12
|
+
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
13
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
14
|
+
[](https://github.com/prettier/prettier)
|
|
15
|
+
[](https://github.com/sponsors/romakita)
|
|
16
|
+
[](https://opencollective.com/tsed)
|
|
17
|
+
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
<div align="center">
|
|
21
|
+
<a href="https://tsed.io/">Website</a>
|
|
22
|
+
<span> • </span>
|
|
23
|
+
<a href="https://tsed.io/getting-started/">Getting started</a>
|
|
24
|
+
<span> • </span>
|
|
25
|
+
<a href="https://api.tsed.io/rest/slack/tsedio/tsed">Slack</a>
|
|
26
|
+
<span> • </span>
|
|
27
|
+
<a href="https://twitter.com/TsED_io">Twitter</a>
|
|
28
|
+
</div>
|
|
29
|
+
|
|
30
|
+
<hr />
|
|
31
|
+
|
|
32
|
+
A package of Ts.ED framework. See website: https://tsed.io
|
|
33
|
+
|
|
34
|
+
## Feature
|
|
35
|
+
|
|
36
|
+
[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/).
|
|
37
|
+
|
|
38
|
+
The `@tsed/temporal` module allows you to decorate classes with `@Temporal` and
|
|
39
|
+
corresponding methods with `@Activity` to write and start asynchronous workflows.
|
|
40
|
+
|
|
41
|
+
Inject the `TemporalClient` to start/schedule and query workflows or to send signals to them.
|
|
42
|
+
|
|
43
|
+
Use the `bootstrapWorker` helper to start a queue that executes your workflows and activities.
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
To begin, install the Temporal module for Ts.ED:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm install --save @tsed/temporal
|
|
51
|
+
npm install --save @temporalio/client @temporalio/worker
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Configure your server
|
|
55
|
+
|
|
56
|
+
Import `@tsed/temporal` in your Server:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import {Configuration} from "@tsed/common";
|
|
60
|
+
import "@tsed/temporal"; // import temporal ts.ed module
|
|
61
|
+
|
|
62
|
+
@Configuration({
|
|
63
|
+
temporal: {
|
|
64
|
+
enabled: true,
|
|
65
|
+
connection: {
|
|
66
|
+
/* optional: see ConnectionOptions of @temporalio/client */
|
|
67
|
+
},
|
|
68
|
+
client: {
|
|
69
|
+
/* optional: see ClientOptions of @temporalio/client */
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
export class Server {}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Create a new Service
|
|
77
|
+
|
|
78
|
+
Decorate the class with `@Temporal`.
|
|
79
|
+
|
|
80
|
+
Use the `@Activity` decorator to define activities.
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import {Temporal, Activity} from "@tsed/temporal";
|
|
84
|
+
|
|
85
|
+
@Temporal()
|
|
86
|
+
export class UserOnboardingActivities {
|
|
87
|
+
constructor(private userService: UserService, private emailService: EmailService) {}
|
|
88
|
+
|
|
89
|
+
@Activity()
|
|
90
|
+
async sendVerificationEmail(email: string) {
|
|
91
|
+
return this.emailService.sendVerificationEmail(email);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@Activity()
|
|
95
|
+
async activateUser(email: string) {
|
|
96
|
+
return this.userService.activateUser(email);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@Activity()
|
|
100
|
+
async sendWelcomeEmail(email: string) {
|
|
101
|
+
return this.emailService.sendWelcomeEmail(email);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@Activity()
|
|
105
|
+
async sendFollowUpEmail(email: string) {
|
|
106
|
+
return this.emailService.sendFollowUpEmail(email);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
@Activity()
|
|
110
|
+
async deleteUser(email: string) {
|
|
111
|
+
return this.userService.deleteUser(email);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Optional, create an interface for your activities to use it later for your [workflows](https://docs.temporal.io/workflows).
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
interface IUserOnboardingActivities {
|
|
120
|
+
sendVerificationEmail(email: string): Promise<void>;
|
|
121
|
+
activateUser(email: string): Promise<void>;
|
|
122
|
+
sendWelcomeEmail(email: string): Promise<void>;
|
|
123
|
+
sendFollowUpEmail(email: string): Promise<void>;
|
|
124
|
+
deleteUser(email: string): Promise<void>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export type Activities = IGreetingActivity;
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Write Workflows
|
|
131
|
+
|
|
132
|
+
Workflows are regular functions that cannot interact directly with Ts.ED or other packages. Just the earlier created interface is used for type-safety.
|
|
133
|
+
|
|
134
|
+
::: warning
|
|
135
|
+
Do not import any non temporal packages here. The workflows are bundled internally by the worker and won't have access to anything else.
|
|
136
|
+
:::
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import {proxyActivities, defineSignal, setHandler, condition, sleep} from "@temporalio/workflow";
|
|
140
|
+
import {Activities} from "../activities";
|
|
141
|
+
|
|
142
|
+
export const isVerifiedSignal = defineSignal("verificationSignal");
|
|
143
|
+
|
|
144
|
+
export async function onboardUser(email: string): Promise<string> {
|
|
145
|
+
const {sendVerificationEmail, activateUser, sendWelcomeEmail, sendFollowUpEmail, deleteUser} = proxyActivities<Activities>({
|
|
146
|
+
startToCloseTimeout: "1 minute"
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
let isVerified = false;
|
|
150
|
+
setHandler(isVerifiedSignal, () => {
|
|
151
|
+
isVerified = true;
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// 1. Send verification email
|
|
155
|
+
await sendVerificationEmail(email);
|
|
156
|
+
|
|
157
|
+
// 2. Wait for verification ...
|
|
158
|
+
const verifiedInTime = await condition(() => isVerified, "1w" /* or for a timeout */);
|
|
159
|
+
if (!verifiedInTime) {
|
|
160
|
+
// 3a. If not verified in time, delete user
|
|
161
|
+
await deleteUserAndTenant(email);
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// 3b. If verified in time, send welcome email
|
|
166
|
+
await sendWelcomeEmail(email);
|
|
167
|
+
|
|
168
|
+
// 4. Send follow up email after one day
|
|
169
|
+
await sleep("1d"); // special sleep function by temporal
|
|
170
|
+
await sendFollowUpEmail(email);
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Inject TemporalClient
|
|
175
|
+
|
|
176
|
+
Inject the TemporalClient instance to interact with it directly, e.g. to start a workflow.
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
import {Service, AfterRoutesInit} from "@tsed/common";
|
|
180
|
+
import {TemporalClient} from "@tsed/temporal";
|
|
181
|
+
import {onboardUser} from "../workflows";
|
|
182
|
+
|
|
183
|
+
@Service()
|
|
184
|
+
export class UsersService implements AfterRoutesInit {
|
|
185
|
+
@Inject()
|
|
186
|
+
private temporalClient: TemporalClient;
|
|
187
|
+
|
|
188
|
+
async create(user: User): Promise<User> {
|
|
189
|
+
// ...
|
|
190
|
+
await this.temporalClient.workflow.start(onboardUser, {
|
|
191
|
+
args: [user.email],
|
|
192
|
+
taskQueue: "onboarding",
|
|
193
|
+
workflowId: `onboarding-${user.id}`
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Start a worker
|
|
200
|
+
|
|
201
|
+
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.
|
|
202
|
+
|
|
203
|
+
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.
|
|
204
|
+
|
|
205
|
+
Read more about it [here](https://docs.temporal.io/typescript/troubleshooting/#webpack-errors).
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
import {bootstrapWorker} from "@tsed/temporal";
|
|
209
|
+
import {Server} from "./app/Server";
|
|
210
|
+
|
|
211
|
+
const worker = await bootstrapWorker(Server, {
|
|
212
|
+
worker: {
|
|
213
|
+
taskQueue: "onboarding",
|
|
214
|
+
workflowsPath: require.resolve("./temporal") // other example: path.join(process.cwd(), 'dist/apps/api/temporal/index.ts');
|
|
215
|
+
},
|
|
216
|
+
connection: {
|
|
217
|
+
/* optional: see NativeConnectionOptions of @temporalio/worker */
|
|
218
|
+
},
|
|
219
|
+
platform: {
|
|
220
|
+
/* optional: see PlatformBuilderSettings of @tsed/common */
|
|
221
|
+
componentsScan: false,
|
|
222
|
+
logger: {
|
|
223
|
+
level: "info"
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
await worker.run();
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Contributors
|
|
231
|
+
|
|
232
|
+
Please read [contributing guidelines here](https://tsed.io/contributing.html)
|
|
233
|
+
|
|
234
|
+
<a href="https://github.com/tsedio/tsed/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
235
|
+
|
|
236
|
+
## Backers
|
|
237
|
+
|
|
238
|
+
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
|
|
239
|
+
|
|
240
|
+
<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/backers.svg?width=890"></a>
|
|
241
|
+
|
|
242
|
+
## Sponsors
|
|
243
|
+
|
|
244
|
+
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
|
|
245
|
+
|
|
246
|
+
## License
|
|
247
|
+
|
|
248
|
+
The MIT License (MIT)
|
|
249
|
+
|
|
250
|
+
Copyright (c) 2016 - 2021 Romain Lenzotti
|
|
251
|
+
|
|
252
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
253
|
+
|
|
254
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
255
|
+
|
|
256
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {classOf} from "@tsed/core";
|
|
2
|
+
import {Logger} from "@tsed/common";
|
|
3
|
+
import {Inject, InjectorService, Module, Provider} from "@tsed/di";
|
|
4
|
+
import {PROVIDER_TYPE_TEMPORAL} from "./constants";
|
|
5
|
+
import {TemporalStore, TEMPORAL_STORE_KEY} from "./interfaces/TemporalStore";
|
|
6
|
+
import {TemporalClient} from "./services/TemporalFactory";
|
|
7
|
+
|
|
8
|
+
@Module()
|
|
9
|
+
export class TemporalModule {
|
|
10
|
+
@Inject()
|
|
11
|
+
protected logger!: Logger;
|
|
12
|
+
|
|
13
|
+
@Inject()
|
|
14
|
+
protected injector!: InjectorService;
|
|
15
|
+
|
|
16
|
+
@Inject()
|
|
17
|
+
protected client!: TemporalClient;
|
|
18
|
+
|
|
19
|
+
constructor(@Inject(PROVIDER_TYPE_TEMPORAL) private temporalServices: any[]) {}
|
|
20
|
+
|
|
21
|
+
public getActivities(): object {
|
|
22
|
+
return this.temporalServices.reduce((activities, instance) => Object.assign(activities, this.getActivitiesFromInstance(instance)), {});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
protected getActivitiesFromInstance(instance: any) {
|
|
26
|
+
const provider = this.injector.getProvider(classOf(instance))!;
|
|
27
|
+
const store = provider.store.get<TemporalStore>(TEMPORAL_STORE_KEY, {});
|
|
28
|
+
|
|
29
|
+
return Object.entries(store.activities || {}).reduce((activities, [propertyKey, {name}]) => {
|
|
30
|
+
const jobProcessor = instance[propertyKey].bind(instance);
|
|
31
|
+
const jobName = name || propertyKey;
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
...activities,
|
|
35
|
+
[jobName]: jobProcessor
|
|
36
|
+
};
|
|
37
|
+
}, {});
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const PROVIDER_TYPE_TEMPORAL = "PROVIDER_TYPE_TEMPORAL";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {Store} from "@tsed/core";
|
|
2
|
+
import {Activity} from "./activity";
|
|
3
|
+
import {Temporal} from "./temporal";
|
|
4
|
+
|
|
5
|
+
describe("@Activity()", () => {
|
|
6
|
+
it("should set metadata", () => {
|
|
7
|
+
@Temporal()
|
|
8
|
+
class Test {
|
|
9
|
+
@Activity()
|
|
10
|
+
testActivityDecorator() {
|
|
11
|
+
// test
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const store = Store.from(Test);
|
|
16
|
+
|
|
17
|
+
expect(store.get("temporal").activities).toEqual({
|
|
18
|
+
testActivityDecorator: {}
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should set options metadata", () => {
|
|
23
|
+
@Temporal()
|
|
24
|
+
class Test {
|
|
25
|
+
@Activity({
|
|
26
|
+
name: "testActivityDecoratorCustomName"
|
|
27
|
+
})
|
|
28
|
+
test() {
|
|
29
|
+
// test
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const store = Store.from(Test);
|
|
34
|
+
expect(store.get("temporal").activities).toEqual({
|
|
35
|
+
test: {
|
|
36
|
+
name: "testActivityDecoratorCustomName"
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {Store} from "@tsed/core";
|
|
2
|
+
import {TemporalStore, ActivityOptions, TEMPORAL_STORE_KEY} from "../interfaces/TemporalStore";
|
|
3
|
+
|
|
4
|
+
export function Activity(options: ActivityOptions = {}): MethodDecorator {
|
|
5
|
+
return (target: any, propertyKey: string | symbol) => {
|
|
6
|
+
const store: TemporalStore = {
|
|
7
|
+
activities: {
|
|
8
|
+
[propertyKey]: options
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
Store.from(target).merge(TEMPORAL_STORE_KEY, store);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {GlobalProviders} from "@tsed/di";
|
|
2
|
+
import {Temporal} from "./temporal";
|
|
3
|
+
|
|
4
|
+
describe("@Activity()", () => {
|
|
5
|
+
it("should set metadata", () => {
|
|
6
|
+
@Temporal()
|
|
7
|
+
class Test {}
|
|
8
|
+
|
|
9
|
+
expect(GlobalProviders.get(Test)?.useClass).toEqual(Test);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {useDecorators} from "@tsed/core";
|
|
2
|
+
import {Injectable} from "@tsed/di";
|
|
3
|
+
import {PROVIDER_TYPE_TEMPORAL} from "../constants";
|
|
4
|
+
|
|
5
|
+
export function Temporal(): ClassDecorator {
|
|
6
|
+
return useDecorators(
|
|
7
|
+
Injectable({
|
|
8
|
+
type: PROVIDER_TYPE_TEMPORAL
|
|
9
|
+
})
|
|
10
|
+
);
|
|
11
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by barrelsby.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export * from "./TemporalModule";
|
|
6
|
+
export * from "./constants";
|
|
7
|
+
export * from "./decorators/activity";
|
|
8
|
+
export * from "./decorators/temporal";
|
|
9
|
+
export * from "./interfaces/TemporalStore";
|
|
10
|
+
export * from "./interfaces/interfaces";
|
|
11
|
+
export * from "./services/TemporalFactory";
|
|
12
|
+
export * from "./utils/worker";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {ClientOptions, ConnectionOptions} from "@temporalio/client";
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
5
|
+
namespace TsED {
|
|
6
|
+
interface Configuration {
|
|
7
|
+
temporal?: {
|
|
8
|
+
enabled?: boolean;
|
|
9
|
+
connection?: ConnectionOptions;
|
|
10
|
+
client?: Omit<ClientOptions, "connection">;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {Configuration, registerProvider} from "@tsed/di";
|
|
2
|
+
import {Client, Connection} from "@temporalio/client";
|
|
3
|
+
import {Logger} from "@tsed/common";
|
|
4
|
+
|
|
5
|
+
export const TemporalConnection = Connection;
|
|
6
|
+
export type TemporalConnection = Connection;
|
|
7
|
+
|
|
8
|
+
export const TemporalClient = Client;
|
|
9
|
+
export type TemporalClient = Client;
|
|
10
|
+
|
|
11
|
+
registerProvider({
|
|
12
|
+
provide: TemporalConnection,
|
|
13
|
+
deps: [Configuration, Logger],
|
|
14
|
+
async useAsyncFactory(settings: Configuration, logger: Logger) {
|
|
15
|
+
const {temporal} = settings;
|
|
16
|
+
if (!temporal?.enabled) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
logger.info("Connecting to Temporal Server: ", temporal.connection?.address || "default");
|
|
22
|
+
const connection = await Connection.connect(temporal.connection);
|
|
23
|
+
logger.info("... connected to Temporal Server: ", temporal.connection?.address || "default");
|
|
24
|
+
return connection;
|
|
25
|
+
} catch (error) {
|
|
26
|
+
logger.error("Failed to connect to Temporal Server:", error);
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
registerProvider({
|
|
33
|
+
provide: TemporalClient,
|
|
34
|
+
deps: [Configuration, TemporalConnection],
|
|
35
|
+
useFactory(settings: Configuration, connection: TemporalConnection) {
|
|
36
|
+
const {temporal} = settings;
|
|
37
|
+
if (!temporal?.enabled) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const client = new TemporalClient({
|
|
42
|
+
connection,
|
|
43
|
+
...temporal.client
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return client;
|
|
47
|
+
},
|
|
48
|
+
hooks: {
|
|
49
|
+
$onDestroy: async (client: Client | null) => {
|
|
50
|
+
if (client) {
|
|
51
|
+
await client.connection.close();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {PlatformBuilderSettings, PlatformTest} from "@tsed/common";
|
|
2
|
+
import {$log} from "@tsed/logger";
|
|
3
|
+
|
|
4
|
+
import {NativeConnection, NativeConnectionOptions, Worker, WorkerOptions} from "@temporalio/worker";
|
|
5
|
+
import {TemporalModule} from "../TemporalModule";
|
|
6
|
+
|
|
7
|
+
type BootstrapWorkerOptions = {
|
|
8
|
+
platform?: PlatformBuilderSettings;
|
|
9
|
+
worker: Omit<WorkerOptions, "activities">;
|
|
10
|
+
connection?: NativeConnection | NativeConnectionOptions;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export async function bootstrapWorker(mod: any, settings: BootstrapWorkerOptions) {
|
|
14
|
+
await PlatformTest.bootstrap(mod, settings.platform)();
|
|
15
|
+
const temporalioModule = PlatformTest.get<TemporalModule>(TemporalModule);
|
|
16
|
+
const activities = temporalioModule.getActivities();
|
|
17
|
+
$log.info('Starting temporal worker with queue "%s" and activities:', settings.worker.taskQueue);
|
|
18
|
+
|
|
19
|
+
if (Object.keys(activities).length > 0) {
|
|
20
|
+
$log.printTable(
|
|
21
|
+
Object.entries(activities).map((x) => ({
|
|
22
|
+
name: x[0],
|
|
23
|
+
activity: x[1].toString()
|
|
24
|
+
}))
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let connection: NativeConnection;
|
|
29
|
+
if (settings.connection instanceof NativeConnection) {
|
|
30
|
+
connection = settings.connection;
|
|
31
|
+
} else {
|
|
32
|
+
$log.info("Connecting to Temporal Server: ", settings.connection?.address || "default");
|
|
33
|
+
connection = await NativeConnection.connect(settings.connection);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const worker = await Worker.create({
|
|
37
|
+
connection,
|
|
38
|
+
...settings.worker,
|
|
39
|
+
activities
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return worker;
|
|
43
|
+
}
|