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
runCancellableActivity
Workflow and retains a handle to it for later use- The
fakeProgress
activity starts and heartbeats
- The
- Waits 40s
- Uses the handle to
cancel()
the workflow- The activity's
sleep
receives the cancellation signal and throws aCancelledFailure
for you to handle - Activity catches the
CancelledFailure
which you can check withisCancellation(err)
- Prints
Fake progress activity cancelled
in the Worker terminal
- Prints
- Workflow prints
Workflow cancelled along with its activity
in the Worker terminal
- The activity's
- Prints
Cancelled workflow successfully
- Calls
result()
demonstrate that callingresult()
on a workflow that is cancelled will throw aWorkflowFailedError
with a cause ofCancelledFailure
. - The
WorkflowFailedError
is caught and we printhandle.result() threw because Workflow was cancelled
temporal server start-dev
to start Temporal Server.npm install
to install dependencies.npm run start
to start the Worker.- In another shell,
npm run workflow
to 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-C
to kill the Worker. - Wait 5 seconds (long enough for
heartbeatTimeout: '3s'
to lapse and the Activity to be rescheduled.) npm run start
to 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