Implement scheduling cancellation support for sagas#9490
Conversation
There was a problem hiding this comment.
Pull request overview
Adds saga-level support for cancelling previously scheduled messages when a saga reaches a final state, by capturing scheduling tokens during scheduled publish/send and cancelling them on completion.
Changes:
- Capture scheduling cancellation tokens returned by
IMessageBus.SchedulePublishAsync/ScheduleSendAsyncand persist them on saga state. - Cancel captured tokens when entering a final saga state (with logging on failure).
- Update/add saga scheduling tests and enhance the test message bus to issue/cancel tokens.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Mocha/test/Mocha.Sagas.Tests/SagaSchedulingTests.cs | Updates existing scheduling tests to assert schedule-token capture (instead of ScheduledTime options). |
| src/Mocha/test/Mocha.Sagas.Tests/SagaSchedulingCancellationTests.cs | Adds new tests verifying tokens are captured and cancelled on saga completion. |
| src/Mocha/test/Mocha.Sagas.TestHelpers/TestMessageBus.cs | Adds token generation for scheduled operations and records cancelled tokens. |
| src/Mocha/src/Mocha/Sagas/State/SagaStateBase.cs | Introduces ScheduleTokens on saga state for persisted cancellation tokens. |
| src/Mocha/src/Mocha/Sagas/Saga.cs | Implements token capture on scheduling and cancellation of tokens on final-state entry (with warning log on exception). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/Mocha/src/Mocha/Sagas/Saga.cs
Outdated
| if (state.ScheduleTokens is { Count: > 0 }) | ||
| { | ||
| var bus = context.GetBus(); | ||
| foreach (var token in state.ScheduleTokens) | ||
| { |
There was a problem hiding this comment.
After attempting cancellation, ScheduleTokens is left intact. If final-state handling is retried (e.g., store delete fails or the message is redelivered), the saga will attempt to cancel the same tokens again and keep persisting them unnecessarily. Consider removing tokens as they are successfully cancelled, or clearing the list once cancellation has been attempted (optionally keeping only failures).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9490 +/- ##
============================
============================
☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
No description provided.