diff --git a/src/modules/Elsa.Http/Activities/SendHttpRequest.cs b/src/modules/Elsa.Http/Activities/SendHttpRequest.cs index bfae9838d..9578bf84a 100644 --- a/src/modules/Elsa.Http/Activities/SendHttpRequest.cs +++ b/src/modules/Elsa.Http/Activities/SendHttpRequest.cs @@ -172,17 +172,7 @@ public abstract class SendHttpRequestBase : Activity /// protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) { - var request = PrepareRequest(context); - var httpClientFactory = context.GetRequiredService(); - var httpClient = httpClientFactory.CreateClient(nameof(SendHttpRequestBase)); - var cancellationToken = context.CancellationToken; - var response = await httpClient.SendAsync(request, cancellationToken); - var parsedContent = await ParseContentAsync(context, response.Content); - - context.Set(Result, response); - context.Set(ParsedContent, parsedContent); - - await HandleResponseAsync(context, response); + await TrySendAsync(context); } /// @@ -190,6 +180,28 @@ public abstract class SendHttpRequestBase : Activity /// protected abstract ValueTask HandleResponseAsync(ActivityExecutionContext context, HttpResponseMessage response); + private async Task TrySendAsync(ActivityExecutionContext context) + { + var request = PrepareRequest(context); + var httpClientFactory = context.GetRequiredService(); + var httpClient = httpClientFactory.CreateClient(nameof(SendHttpRequestBase)); + var cancellationToken = context.CancellationToken; + + try + { + var response = await httpClient.SendAsync(request, cancellationToken); + var parsedContent = await ParseContentAsync(context, response.Content); + context.Set(Result, response); + context.Set(ParsedContent, parsedContent); + + await HandleResponseAsync(context, response); + } + catch (TaskCanceledException e) + { + context.JournalData.Add("Cancelled", true); + } + } + private async Task ParseContentAsync(ActivityExecutionContext context, HttpContent httpContent) { if (!HasContent(httpContent)) diff --git a/src/modules/Elsa.Http/Handlers/UpdateRouteTable.cs b/src/modules/Elsa.Http/Handlers/UpdateRouteTable.cs index 12b25e2be..44e098a38 100644 --- a/src/modules/Elsa.Http/Handlers/UpdateRouteTable.cs +++ b/src/modules/Elsa.Http/Handlers/UpdateRouteTable.cs @@ -5,13 +5,21 @@ using Elsa.Workflows.Runtime.Notifications; namespace Elsa.Http.Handlers; +/// +/// A handler that updates the route table. +/// public class UpdateRouteTable : INotificationHandler, INotificationHandler { private readonly IRouteTable _routeTable; + + /// + /// Initializes a new instance of the class. + /// public UpdateRouteTable(IRouteTable routeTable) => _routeTable = routeTable; + /// public Task HandleAsync(WorkflowTriggersIndexed notification, CancellationToken cancellationToken) { _routeTable.RemoveRoutes(notification.IndexedWorkflowTriggers.RemovedTriggers); @@ -20,6 +28,7 @@ public class UpdateRouteTable : return Task.CompletedTask; } + /// public Task HandleAsync(WorkflowBookmarksIndexed notification, CancellationToken cancellationToken) { _routeTable.RemoveRoutes(notification.IndexedWorkflowBookmarks.RemovedBookmarks); diff --git a/src/modules/Elsa.Workflows.Runtime/Services/DefaultBackgroundActivityInvoker.cs b/src/modules/Elsa.Workflows.Runtime/Services/DefaultBackgroundActivityInvoker.cs index 0c69ec01a..a7f9baa59 100644 --- a/src/modules/Elsa.Workflows.Runtime/Services/DefaultBackgroundActivityInvoker.cs +++ b/src/modules/Elsa.Workflows.Runtime/Services/DefaultBackgroundActivityInvoker.cs @@ -6,6 +6,7 @@ using Elsa.Workflows.Runtime.Contracts; using Elsa.Workflows.Runtime.Middleware.Activities; using Elsa.Workflows.Runtime.Models; using Elsa.Workflows.Runtime.Models.Requests; +using Microsoft.Extensions.Logging; namespace Elsa.Workflows.Runtime.Services; @@ -21,6 +22,7 @@ public class DefaultBackgroundActivityInvoker : IBackgroundActivityInvoker private readonly IVariablePersistenceManager _variablePersistenceManager; private readonly IActivityInvoker _activityInvoker; private readonly IServiceProvider _serviceProvider; + private readonly ILogger _logger; /// /// Initializes a new instance of the class. @@ -32,7 +34,8 @@ public class DefaultBackgroundActivityInvoker : IBackgroundActivityInvoker IWorkflowExecutionContextFactory workflowExecutionContextFactory, IVariablePersistenceManager variablePersistenceManager, IActivityInvoker activityInvoker, - IServiceProvider serviceProvider) + IServiceProvider serviceProvider, + ILogger logger) { _workflowRuntime = workflowRuntime; _workflowDispatcher = workflowDispatcher; @@ -41,6 +44,7 @@ public class DefaultBackgroundActivityInvoker : IBackgroundActivityInvoker _variablePersistenceManager = variablePersistenceManager; _activityInvoker = activityInvoker; _serviceProvider = serviceProvider; + _logger = logger; } /// @@ -118,6 +122,12 @@ public class DefaultBackgroundActivityInvoker : IBackgroundActivityInvoker } }; + if(cancellationToken.IsCancellationRequested) + { + _logger.LogInformation("Background execution for activity {ActivityNodeId} was canceled", activityNodeId); + return; + } + await _workflowDispatcher.DispatchAsync(dispatchRequest, cancellationToken); } } \ No newline at end of file