How to cancel an activity?

I know it can cancel executing activity by cancelWorkflow API , my question is to cancel a specific activity by signal or something else, instead of cancelling a specific activity via the parent workflow. Are there some examples that I can learn from.

1 Like

Hi @haojie

One thing you can do is to create a signalMethod in the parentWorkflow and cancel the activity from there, see this example temporal-cancel-activity/TemporalWorkflow.java at main · tsurdilo/temporal-cancel-activity · GitHub

I hope this helps.

my question is to cancel a specific activity by signal

Just to add, for Go you could use workflow.WithCancel (see sample https://github.com/temporalio/samples-go/blob/main/pickfirst/pickfirst_workflow.go):

activityCtx, cancelActivity := workflow.WithCancel(ctx)

and invoke your activity that can be cancelled with activityCtx. When you receive a signal call
cancelActivity() to cancel. Note your activity has to heartbeat to receive cancellation requests (will be receive on next heartbeat). You can also set ActivityOptions->WaitForCancellation to true if you need to you wait for canceled activity to be completed.