elsa-core/src/modules/Elsa.Workflows.Runtime/Services/BookmarkQueueSignaler.cs
Sipke Schoorstra 2dcd852833
Enhances workflow runtime resilience and signaling (#6643)
* Refactor BookmarkQueueSignaler to use Channel for signaling.

Replaced TaskCompletionSource with a bounded Channel to improve concurrency control and simplify the code. This change ensures better handling of multiple producers while maintaining a single reader model.

* Refactor BookmarkQueueWorker to improve resilience and clarity

Replaced Debouncer with Throttler for rate limiting and added error handling to log exceptions and ensure the worker loop continues safely while allowing proper shutdown on cancellation.

* Handle missing workflow instance during bookmark resumption

Add exception handling for `WorkflowInstanceNotFoundException` to handle cases where the workflow instance does not exist. Log a debug message and return `ResumeBookmarkResult.NotFound()` when such instances are encountered. This ensures better error management and logging for bookmark resumption.

* Refactor default initializations and rename completion methods.

Replaced `default!` with `null!` for input properties to improve clarity and consistency. Renamed methods to better reflect their purpose, changing `CheckIfCompletedAsync` to `AttemptToCompleteAsync`. These changes enhance code readability and maintainability.

* Refactor to use specific exceptions for workflow errors

Replaced generic `InvalidOperationException` with `WorkflowInstanceNotFoundException` and `WorkflowGraphNotFoundException` for improved error context. This enhances clarity and enables more precise error handling.

* Change default value of WorkflowInstanceId to null

Updated the property `WorkflowInstanceId` to use `null!` instead of `default!` to better align with nullable reference type semantics. This ensures clarity and consistency in the codebase regarding expected default values.

* Add handling for WorkflowInstanceSaved in SignalBookmarkQueueWorker

Updated the SignalBookmarkQueueWorker to implement INotificationHandler for WorkflowInstanceSaved. This ensures that workflow instance save events now trigger the bookmark queue worker, improving event handling consistency.

* Update comment to clarify bookmark and workflow instance check

Expanded the comment to explain that the queue item is stored not only when a bookmark is missing but also when the associated workflow instance is not yet in the database. This improves clarity for future maintainers regarding queuing conditions.
2025-05-13 13:51:47 +02:00

30 lines
778 B
C#

using System.Threading.Channels;
namespace Elsa.Workflows.Runtime;
public class BookmarkQueueSignaler : IBookmarkQueueSignaler
{
private readonly Channel<object?> _channel;
public BookmarkQueueSignaler()
{
var options = new BoundedChannelOptions(1)
{
SingleReader = true,
SingleWriter = false,
AllowSynchronousContinuations = false
};
_channel = Channel.CreateBounded<object?>(options);
}
public Task AwaitAsync(CancellationToken cancellationToken)
{
return _channel.Reader.ReadAsync(cancellationToken).AsTask();
}
public Task TriggerAsync(CancellationToken cancellationToken)
{
_channel.Writer.TryWrite(null);
return Task.CompletedTask;
}
}