Skip to content

Commit

Permalink
handle cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
lorensr committed Mar 28, 2024
1 parent 33c379e commit eb623e3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Prerequisites:
* [Polling](src/Polling) - Recommended implementation of an activity that needs to periodically poll an external resource waiting its successful completion.
* [Saga](src/Saga) - Demonstrates how to implement a saga pattern.
* [Schedules](src/Schedules) - How to schedule workflows to be run at specific times in the future.
* [Timer](src/Timer) - Use a timer to implement a monthly subscription.
* [Timer](src/Timer) - Use a timer to implement a monthly subscription; handle workflow cancellation.
* [WorkerSpecificTaskQueues](src/WorkerSpecificTaskQueues) - Use a unique task queue per Worker to have certain Activities only run on that specific Worker.
* [WorkerVersioning](src/WorkerVersioning) - How to use the Worker Versioning feature to more easily deploy changes to Workflow & other code.
* [WorkflowUpdate](src/WorkflowUpdate) - How to use the Workflow Update feature while blocking in update method for concurrent updates.
Expand Down
2 changes: 1 addition & 1 deletion src/Timer/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Timer

Use a timer (`Workflow.DelayAsync`) to implement a monthly subscription.
Use a timer (`Workflow.DelayAsync`) to implement a monthly subscription. Also, handle workflow cancellation.

To run, first see [README.md](../../README.md) for prerequisites. Then, run the following from this directory
in a separate terminal to start the worker:
Expand Down
22 changes: 16 additions & 6 deletions src/Timer/Subscription.workflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ public class Subscription
[WorkflowRun]
public async Task RunAsync(string userId)
{
while (true)
try
{
await Workflow.DelayAsync(TimeSpan.FromDays(30));
while (true)
{
await Workflow.DelayAsync(TimeSpan.FromDays(30));

var result = await Workflow.ExecuteActivityAsync(
() => MyActivities.Charge(userId),
new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) });
Workflow.Logger.LogInformation("Activity result: {Result}", result);
var result = await Workflow.ExecuteActivityAsync(
() => MyActivities.Charge(userId),
new() { StartToCloseTimeout = TimeSpan.FromMinutes(5) });
Workflow.Logger.LogInformation("Activity result: {Result}", result);
}
}
catch (OperationCanceledException)
{
Workflow.Logger.LogInformation("Workflow cancelled, cleaning up...");
// Handle any cleanup here
// Re-throw to close the workflow as Cancelled. Otherwise, it will be closed as Completed.
throw;
}
}
}

0 comments on commit eb623e3

Please sign in to comment.