new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Temporal · Develop · all subjects

async activity completion

20 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Mark Activity as completing asynchronously in .NET

To mark an Activity as completing asynchronously in .NET SDK, capture the task token from ActivityExecutionContext.Current.Info.TaskToken and throw a CompleteAsyncException() to signal that the Activity will be completed elsewhere.

Methods available on async activity handle in .NET

On an async activity handle obtained via GetAsyncActivityHandle(), you can call CompleteAsync() to complete the Activity with a value, HeartbeatAsync() to send a heartbeat, FailAsync() to fail the Activity, or ReportCancellationAsync() to report cancellation.

Complete async Activity with value in .NET

To complete an asynchronously-executing Activity in .NET, call await handle.CompleteAsync("Completion value.") on the activity handle, passing the completion value as an argument.

doNotCompleteOnReturn() prevents immediate activity completion

Call context.doNotCompleteOnReturn() during an Activity Execution to prevent the Activity Execution from completing when its method returns. When this method is called, the workflow action method return value is ignored.

ActivityCompletionClient.complete() method

Use completionClient.complete(taskToken, result) to complete an activity asynchronously. The taskToken is the byte array obtained from the activity execution context, and result is the activity return value.

Three steps for asynchronous activity completion

To asynchronously complete an Activity in Java SDK: (1) The Activity provides the external system with identifying information needed to complete the Activity Execution, which can be a Task Token or a combination of Namespace, Workflow Id, and Activity Id. (2) The Activity Function completes in a way that identifies it as waiting to be completed by an external system. (3) The Temporal Client is used to Heartbeat and complete the Activity.

Identifying information for async activity completion

When completing an Activity asynchronously, the identifying information can be either a Task Token or a combination of Namespace, Workflow Id, and Activity Id.

Async activity completion example with ForkJoinPool

Example showing asynchronous activity completion in Java SDK: ```java @Override public String composeGreeting(String greeting, String name) { // Get the activity execution context ActivityExecutionContext context = Activity.getExecutionContext(); // Set a correlation token that can be used to complete the activity asynchronously byte[] taskToken = context.getTaskToken(); // Execute asynchronously ForkJoinPool.commonPool().execute(() -> composeGreetingAsync(taskToken, greeting, name)); context.doNotCompleteOnReturn(); // Since we have set doNotCompleteOnReturn(), the workflow action method return value is // ignored. return "ignored"; } // Method that will complete action execution using the defined ActivityCompletionClient private void composeGreetingAsync(byte[] taskToken, String greeting, String name) { String result = greeting + " " + name + "!"; // Complete our workflow activity using ActivityCompletionClient completionClient.complete(taskToken, result); } ``` This example demonstrates passing a task token to an asynchronous operation (ForkJoinPool), calling doNotCompleteOnReturn() to prevent immediate completion, and then using ActivityCompletionClient.complete() to finish the activity from the async method.

ActivityCompletionClient for async activity completion

Set the ActivityCompletionClient interface to the complete() method to complete an Activity asynchronously in Java SDK.

Get task token from activity execution context

Call Activity.getExecutionContext() to obtain the ActivityExecutionContext, then call context.getTaskToken() to retrieve the task token as a byte array. This token can be used as a correlation identifier to complete the activity asynchronously.

Three steps for asynchronous Activity completion

Asynchronous Activity completion requires three steps: (1) The Activity provides the external system with identifying information needed to complete the Activity Execution, which can be a Task Token or a combination of Namespace, Workflow Id, and Activity Id. (2) The Activity Function completes in a way that identifies it as waiting to be completed by an external system. (3) The Temporal Client is used to Heartbeat and complete the Activity.

Mark an Activity for asynchronous completion in Python

To mark an Activity as completing asynchronously in the Python SDK, capture the task token using activity.info().task_token and then call activity.raise_complete_async() inside the Activity.

Get async activity handle with task token

Use the get_async_activity_handle() method on the Temporal Client with the task_token parameter to get the handle of an asynchronously-completing Activity. For example: handle = my_client.get_async_activity_handle(task_token=captured_token)

Complete asynchronous Activity with handle

Once you have an async activity handle, call the complete() method to complete the Activity with a value. For example: await handle.complete('Completion value.') You can also use heartbeat(), fail(), or report_cancellation() methods on the handle.

Asynchronous Activity completion example - Python

The following example shows how to mark an Activity for asynchronous completion and later complete it from outside the Activity: # Inside the Activity captured_token = activity.info().task_token activity.raise_complete_async() # Outside the Activity, using the Temporal Client handle = my_client.get_async_activity_handle(task_token=captured_token) await handle.complete("Completion value.")

Asynchronous Activity completion three-step process

Asynchronous Activity Completion enables the Activity Function to return without the Activity Execution completing. The three steps are: (1) The Activity provides the external system with identifying information needed to complete the Activity Execution, which can be a Task Token or a combination of Namespace, Workflow Id, and Activity Id. (2) The Activity Function completes in a way that identifies it as waiting to be completed by an external system. (3) The Temporal Client is used to Heartbeat and complete the Activity.

AsyncCompletionClient.complete method for async activities

To asynchronously complete an Activity in TypeScript, call AsyncCompletionClient.complete with the task token and the activity result.

Throw CompleteAsyncError to mark activity as waiting for external completion

In TypeScript, throw a CompleteAsyncError from the Activity Function to identify it as waiting to be completed by an external system.

Get task token from activityInfo in TypeScript

Retrieve the task token for an Activity in TypeScript by calling activityInfo().taskToken, which returns a Uint8Array that can be passed to an external system for async completion.

Async activity completion example in TypeScript

The following example shows how to asynchronously complete an Activity in TypeScript: ```ts import { CompleteAsyncError, activityInfo } from '@temporalio/activity'; import { Client } 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 Client(); // does some work... await client.activity.complete(taskToken, "Job's done!"); } ```

Give your agent this brain