Skip to content

Latest commit

 

History

History
60 lines (45 loc) · 1.76 KB

File metadata and controls

60 lines (45 loc) · 1.76 KB

Cancel Activity Sample

This sample demonstrates graceful activity cancellation with cleanup operations.

Looking for a visual guide? See new_samples/hello_world for screenshots.

How It Works

┌──────────────────┐
│ Start Workflow   │
└────────┬─────────┘
         ▼
┌──────────────────┐     ┌──────────────────┐
│ activityToBe     │────▶│ User cancels     │
│ Canceled()       │     │ workflow         │
│ (heartbeating)   │     └────────┬─────────┘
└──────────────────┘              │
         │◀───────────────────────┘
         ▼
┌──────────────────┐
│ cleanupActivity()│  (runs via NewDisconnectedContext)
└──────────────────┘

Prerequisites

  1. Cadence server running (see main README)
  2. Build the samples: make

Running the Sample

# Terminal 1: Start worker
./bin/cancelactivity -m worker

# Terminal 2: Trigger workflow
./bin/cancelactivity -m trigger

# Terminal 3: Cancel the workflow (copy WorkflowID from trigger output)
./bin/cancelactivity -m cancel -w <WorkflowID>

Key Code

defer func() {
    if cadence.IsCanceledError(retError) {
        newCtx, _ := workflow.NewDisconnectedContext(ctx)
        workflow.ExecuteActivity(newCtx, cleanupActivity).Get(ctx, nil)
    }
}()

Testing

go test -v ./cmd/samples/recipes/cancelactivity/