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.

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/ 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 samples-go/pickfirst_workflow.go at main · temporalio/samples-go · GitHub):

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.