Skip to content

Commit ee28973

Browse files
committed
Add README for cmd/samples/recipes/timer
Signed-off-by: Diana Zawadzki <[email protected]>
1 parent b890413 commit ee28973

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Timer Sample
2+
3+
This sample demonstrates **timer-based notifications** - running a long operation with a timeout that triggers an alert if processing takes too long.
4+
5+
> **Looking for a visual guide?** See [new_samples/hello_world](../../../../new_samples/hello_world/) for a step-by-step tutorial with screenshots.
6+
7+
## How It Works
8+
9+
```
10+
┌──────────────────────────────────────────────────────────────┐
11+
│ Workflow Start │
12+
└──────────────────────────────────────────────────────────────┘
13+
14+
┌───────────────┴───────────────┐
15+
▼ ▼
16+
┌─────────────────────┐ ┌─────────────────────┐
17+
│ orderProcessing() │ │ Timer (3 seconds) │
18+
│ (random 0-10 sec) │ │ │
19+
└─────────────────────┘ └─────────────────────┘
20+
│ │
21+
│ If timer fires first: │
22+
│ ◀─────────────────────────────
23+
│ sendEmail() │
24+
│ │
25+
▼ │
26+
┌─────────────────────┐ │
27+
│ Wait for processing │◀───────────────────┘
28+
│ to complete │
29+
└─────────────────────┘
30+
```
31+
32+
**Use case:** Order processing with SLA monitoring - notify customer if processing is delayed, but don't cancel the operation.
33+
34+
## Prerequisites
35+
36+
1. Cadence server running (see [main README](../../../../README.md))
37+
2. Build the samples: `make`
38+
39+
## Running the Sample
40+
41+
```bash
42+
# Terminal 1: Start worker
43+
./bin/timer -m worker
44+
45+
# Terminal 2: Trigger workflow
46+
./bin/timer -m trigger
47+
```
48+
49+
**Possible outcomes:**
50+
- Processing finishes in < 3 seconds → Timer cancelled, no email
51+
- Processing takes > 3 seconds → Email sent, then wait for completion
52+
53+
## Key Code
54+
55+
```go
56+
// Start long-running activity
57+
f := workflow.ExecuteActivity(ctx, orderProcessingActivity)
58+
selector.AddFuture(f, func(f workflow.Future) {
59+
processingDone = true
60+
cancelHandler() // Cancel timer if processing finishes first
61+
})
62+
63+
// Start timer for notification threshold
64+
timerFuture := workflow.NewTimer(childCtx, processingTimeThreshold)
65+
selector.AddFuture(timerFuture, func(f workflow.Future) {
66+
if !processingDone {
67+
workflow.ExecuteActivity(ctx, sendEmailActivity) // Send notification
68+
}
69+
})
70+
71+
selector.Select(ctx) // Wait for either to complete
72+
```
73+
74+
## Testing
75+
76+
```bash
77+
go test -v ./cmd/samples/recipes/timer/
78+
```
79+
80+
## References
81+
82+
- [Cadence Timers](https://cadenceworkflow.io/docs/go-client/timers/)
83+
- [Workflow Selectors](https://cadenceworkflow.io/docs/go-client/selectors/)
84+

0 commit comments

Comments
 (0)