Asynchronous Activity Completion - TypeScript SDK
How to asynchronously complete an Activity
Asynchronous Activity Completion enables the Activity Function to return without the Activity Execution completing.
There are three steps to follow:
- The Activity provides the external system with identifying information needed to complete the Activity Execution. Identifying information can be a Task Token, or a combination of Namespace, Workflow Id, and Activity Id.
- The Activity Function completes in a way that identifies it as waiting to be completed by an external system.
- The Temporal Client is used to Heartbeat and complete the Activity.
To asynchronously complete an Activity, call AsyncCompletionClient.complete
.
activities-examples/src/activities/async-completion.ts
import { activityInfo, CompleteAsyncError } from '@temporalio/activity';
import { AsyncCompletionClient } from '@temporalio/client';
export async function doSomethingAsync(): Promise<string> {
const taskToken = activityInfo().taskToken;
setTimeout(() => doSomeWork(taskToken), 1000);
throw new CompleteAsyncError();
}
// this work could be done in a different process or on a different machine
async function doSomeWork(taskToken: Uint8Array): Promise<void> {
const client = new AsyncCompletionClient();
// does some work...
await client.complete(taskToken, 'Job\'s done!');
}
Local Activities
To call Local Activities in TypeScript, use proxyLocalActivities
.
import * as workflow from '@temporalio/workflow';
const { getEnvVar } = workflow.proxyLocalActivities({
startToCloseTimeout: '2 seconds',
});
export async function yourWorkflow(): Promise<void> {
const someSetting = await getEnvVar('SOME_SETTING');
// ...
}
Local Activities must be registered with the Worker the same way non-local Activities are.