Ts.ED Plus Temporal
Support for Temporal in your Ts.ED application.
Contents
Prerequisite
If you have not already, familiarize yourself with the Ts.ED framework and the concepts around Temporal
Getting Started
Install Module
# npm
npm install @tsed-plus/temporal
# yarn
yarn add @tsed-plus/temporal
Write Activities
Annotate your class with @Temporal
and the activity methods with @Activity
, given an optional name. You can use your injected services within the activity method.
import { Activity, Temporal } from '@tsed-plus/temporal';
import { GreetingService } from './GreetingService';
@Temporal()
export class GreetingActivities {
constructor(private greetingService: GreetingService) {}
@Activity({ name: 'greeting' })
async greeting(name: string): Promise<string> {
return this.greetingService.greeting(name);
}
}
Optional, create an interface for your activities to use it later for your workflows
interface IGreetingActivity {
greeting(name: string): Promise<string>;
}
export type Activities = IGreetingActivity;
Write Workflows
Workflows are regular functions and do not interact directly with Ts.ED. Just the earlier created interface is used for type-safety.
import { proxyActivities } from '@temporalio/workflow';
import { Activities } from '../activities';
export async function example(name: string): Promise<string> {
const { greeting } = proxyActivities<Activities>({
startToCloseTimeout: '1 minute',
});
return await greeting(name);
}
Start a Workflow
Inject the TemporalWorkflowClient
in a service/controller of your choice and call start
. You can specify the workflow name by string like above or also import the function and pass it to have type-safety for the arguments.
Also check out the Temporal client documentation here about more cool things you can do with the Workflow Clients.
import { nanoid } from 'nanoid';
import { Controller } from '@tsed/di';
import { Get } from '@tsed/schema';
import { QueryParams } from '@tsed/platform-params';
import { TemporalWorkflowClient } from '@tsed-plus/temporal';
@Controller('/')
export class HelloWorldController {
constructor(private workflowClient: TemporalWorkflowClient) {}
@Get('/')
async get(@QueryParams('name') name?: string) {
const workflow = await this.workflowClient.start('example', {
args: [name || 'World'],
taskQueue: 'hello-world',
workflowId: 'workflow-' + nanoid(),
});
return workflow.result();
}
}
Start a worker
The workflows and activities won't get executed until you start a worker.
The most tricky part is the workflowsPath
parameter. This is the path of the file where the workflows are defined. The file is automatically loaded when the worker is started and internally bundled with webpack. Read more about it here.
import { bootstrapWorker } from '@tsed-plus/temporal';
import { Server } from './app/Server';
const worker = await bootstrapWorker(Server, {
worker: {
taskQueue: 'hello-world',
workflowsPath: require.resolve('./temporal'),
},
});
await worker.run();
Example
Look at the puzzle pieces explained above in a working example application.