|
| 1 | +# Retry Activity Sample |
| 2 | + |
| 3 | +This sample demonstrates **activity retry with heartbeat progress** - automatic retries with resume from last checkpoint. |
| 4 | + |
| 5 | +> **Looking for a visual guide?** See [new_samples/hello_world](../../../../new_samples/hello_world/) for screenshots. |
| 6 | +
|
| 7 | +## How It Works |
| 8 | + |
| 9 | +``` |
| 10 | +┌────────────────────────────────────────────────┐ |
| 11 | +│ Batch Processing (20 items) │ |
| 12 | +│ │ |
| 13 | +│ Attempt 1: Process 1-6 → FAIL (simulated) │ |
| 14 | +│ Heartbeat: item 6 ✓ │ |
| 15 | +│ │ |
| 16 | +│ Attempt 2: Resume from 7 → Process 7-12 → FAIL│ |
| 17 | +│ Heartbeat: item 12 ✓ │ |
| 18 | +│ │ |
| 19 | +│ Attempt 3: Resume from 13 → Process 13-20 ✓ │ |
| 20 | +└────────────────────────────────────────────────┘ |
| 21 | +``` |
| 22 | + |
| 23 | +**Use case:** Batch processing, unreliable external APIs, resumable long-running tasks. |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +1. Cadence server running (see [main README](../../../../README.md)) |
| 28 | +2. Build the samples: `make` |
| 29 | + |
| 30 | +## Running the Sample |
| 31 | + |
| 32 | +```bash |
| 33 | +# Terminal 1: Start worker |
| 34 | +./bin/retryactivity -m worker |
| 35 | + |
| 36 | +# Terminal 2: Trigger workflow |
| 37 | +./bin/retryactivity -m trigger |
| 38 | +``` |
| 39 | + |
| 40 | +## Key Code |
| 41 | + |
| 42 | +```go |
| 43 | +RetryPolicy: &cadence.RetryPolicy{ |
| 44 | + InitialInterval: time.Second, |
| 45 | + BackoffCoefficient: 2.0, |
| 46 | + MaximumAttempts: 5, |
| 47 | +} |
| 48 | + |
| 49 | +// Resume from heartbeat progress |
| 50 | +if activity.HasHeartbeatDetails(ctx) { |
| 51 | + activity.GetHeartbeatDetails(ctx, &completedIdx) |
| 52 | + i = completedIdx + 1 // Resume from last checkpoint |
| 53 | +} |
| 54 | + |
| 55 | +activity.RecordHeartbeat(ctx, i) // Save progress |
| 56 | +``` |
| 57 | + |
| 58 | +## Testing |
| 59 | + |
| 60 | +```bash |
| 61 | +go test -v ./cmd/samples/recipes/retryactivity/ |
| 62 | +``` |
| 63 | + |
0 commit comments