How Do I Terminate All Tasks in an Activity?

Currently, I use an @ActivityInterface activity to invoke processbuilder to execute a script. When I terminal the activity, the script invoking does not stop. How can I do this?

The only way for an activity to learn about its cancellation is through heartbeating. So specify a heartbeat timeout and make sure that the activity calls the heartbeat API more frequently than the timeout.

If you are using Go SDK, then make sure that the activity code handles context cancellation. This still requires periodic heartbeating.

When heartbeating is used, the exception ActivityCompletionException is thrown. As a result, the activity stops, but the invoked script is still running.On port 8080, the workflow has failed.

Do you mean to execute the compensation policy after canceling?

You control the activity code. So your code has to stop the script when an ActivityCompletionException is thrown.

In an activity, a runJob is used to execute the entire job. The runJob invokes a runCMD method to execute the script. The @ActivityMethod annotation is used. Now, the runJob throws the ActivityCompletionException exception. However, cancel does not terminate the script invoking in runCMD, and the script invoking process can still be viewed on the remote host.

Sorry, I don’t understand. Temporal doesn’t know anything about the activity implementation. All it can do is notify the activity implementation about its cancellation by throwing ActivityCompletionException from the heartbeat method. It is up to the activity implementation to handle it correctly by terminating whatever scripts it invoked.

The ActivityCompletionException exception is thrown, and the cancelation also changes the status of the workflow to which the activity belongs to to failed. However, the script invoking output is printed on the console after a while. Here, the reading output is on another thread.

Catch the exception and terminate the script before rethrowing it.

You mean to edit my own code to end script execution, rather than automatically terminal after the activity ends.

Yes, the Temporal SDK doesn’t control threads that your activity started.

Thank you very much