Asynchronous Activity Completion
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.
typescript-sandbox/src/activities/async-completion/activities.ts
import { CompleteAsyncError, activityInfo } from '@temporalio/activity';
import { Client, Connection } from '@temporalio/client';
import { loadClientConnectConfig } from '@temporalio/envconfig';
export async function doSomethingAsync(): Promise<string> {
const taskToken = activityInfo().taskToken;
setTimeout(() => void 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 config = loadClientConnectConfig();
const connection = await Connection.connect(config.connectionOptions);
try {
const client = new Client({ connection, namespace: config.namespace });
// does some work...
await client.activity.complete(taskToken, "Job's done!");
} finally {
await connection.close();
}
}
Local Activities
To call Local Activities in TypeScript, use proxyLocalActivities.
typescript-sandbox/src/activities/local-activities/workflows.ts
import * as workflow from '@temporalio/workflow';
import type * as activities from './activities';
const { getEnvVar } = workflow.proxyLocalActivities<typeof activities>({
startToCloseTimeout: '2 seconds',
});
export async function yourWorkflow(): Promise<string | undefined> {
const someSetting = await getEnvVar('SOME_SETTING');
// ...
return someSetting;
}
Local Activities must be registered with the Worker the same way non-local Activities are.