This sample demonstrates:
- How a retried Activity Task can resume from the last Activity Task's heartbeat.
- How to handle canceling a long-running Activity when its associated Workflow is canceled.
Docs: Activity heartbeating and cancellation
Running src/client.ts does this:
- Starts the
runCancellableActivityWorkflow and retains a handle to it for later use- The
fakeProgressactivity starts and heartbeats
- The
- Waits 40s
- Uses the handle to
cancel()the workflow- The activity's
sleepreceives the cancellation signal and throws aCancelledFailurefor you to handle - Activity catches the
CancelledFailurewhich you can check withisCancellation(err)- Prints
Fake progress activity cancelledin the Worker terminal
- Prints
- Workflow prints
Workflow cancelled along with its activityin the Worker terminal
- The activity's
- Prints
Cancelled workflow successfully - Calls
result()demonstrate that callingresult()on a workflow that is cancelled will throw aWorkflowFailedErrorwith a cause ofCancelledFailure. - The
WorkflowFailedErroris caught and we printhandle.result() threw because Workflow was cancelled
temporal server start-devto start Temporal Server.npm installto install dependencies (orpnpmoryarn).npm run startto start the Worker.- In another shell,
npm run workflowto run the Workflow. - In the first shell, watch the Worker output for a few seconds:
Starting activity at progress: 1
Progress: 1
Progress: 2
Progress: 3
Progress: 4
Progress: 5
Progress: 6
Progress: 7
^C
- Hit
Ctrl-Cto kill the Worker. - Wait 5 seconds (long enough for
heartbeatTimeout: '3s'to lapse and the Activity to be rescheduled.) npm run startto start the Worker again:
Starting activity at progress: 4
Progress: 4
Progress: 5
Progress: 6
Progress: 7
Progress: 8
Progress: 9
Progress: 10
Progress: 11
Progress: 12
Progress: 13
Progress: 14
Progress: 15
Progress: 16
Progress: 17
Progress: 18
Progress: 19
Progress: 20
Progress: 21
Progress: 22
Progress: 23
Progress: 24
Fake progress activity cancelled
[runCancellableActivity(cancellation-heartbeating-0)] Workflow cancelled along with its activity
- In the other shell, where the client code is running (
npm run workflow), the output should be:
Cancelled workflow successfully
handle.result() threw because Workflow was cancelled