From eb623e34608766bd95949583db9e848585a60a6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Loren=20=F0=9F=A4=93?= Date: Thu, 28 Mar 2024 19:16:14 -0400 Subject: [PATCH] handle cancellation --- README.md | 2 +- src/Timer/README.md | 2 +- src/Timer/Subscription.workflow.cs | 22 ++++++++++++++++------ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 059e499..2949982 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Timer/README.md b/src/Timer/README.md index afd51a8..d2cb03f 100644 --- a/src/Timer/README.md +++ b/src/Timer/README.md @@ -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: diff --git a/src/Timer/Subscription.workflow.cs b/src/Timer/Subscription.workflow.cs index 2f621ba..9d6bf24 100644 --- a/src/Timer/Subscription.workflow.cs +++ b/src/Timer/Subscription.workflow.cs @@ -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; } } } \ No newline at end of file