Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exceptions generated from ActivityTypeProviders #4033

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/core/Elsa.Core/Services/Bookmarks/BookmarkIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,26 @@ private async Task<IEnumerable<Bookmark>> FindBookmarksAsync(string workflowInst

foreach (var blockingActivity in blockingActivities)
{
var activityExecutionContext = new ActivityExecutionContext(_serviceProvider, workflowExecutionContext, blockingActivity, null, false, cancellationToken);
var activityType = activityTypes[blockingActivity.Type];
var providerContext = new BookmarkProviderContext(activityExecutionContext, activityType, BookmarkIndexingMode.WorkflowInstance);
var providers = await FilterProvidersAsync(providerContext).ToListAsync(cancellationToken);

foreach (var provider in providers)
if(activityTypes.ContainsKey(blockingActivity.Type))
{
var bookmarkResults = (await provider.GetBookmarksAsync(providerContext, cancellationToken)).ToList();
var activityExecutionContext = new ActivityExecutionContext(_serviceProvider, workflowExecutionContext, blockingActivity, null, false, cancellationToken);
var activityType = activityTypes[blockingActivity.Type];
var providerContext = new BookmarkProviderContext(activityExecutionContext, activityType, BookmarkIndexingMode.WorkflowInstance);
var providers = await FilterProvidersAsync(providerContext).ToListAsync(cancellationToken);

bookmarkedWorkflows.AddRange(bookmarkResults.Select(bookmarkResult => new BookmarkedWorkflow
foreach (var provider in providers)
{
WorkflowBlueprint = workflowBlueprint,
WorkflowInstanceId = workflowInstance.Id,
ActivityType = bookmarkResult.ActivityTypeName ?? blockingActivity.Type,
ActivityId = blockingActivity.Id,
Bookmark = bookmarkResult.Bookmark
}));
var bookmarkResults = (await provider.GetBookmarksAsync(providerContext, cancellationToken)).ToList();

bookmarkedWorkflows.AddRange(bookmarkResults.Select(bookmarkResult => new BookmarkedWorkflow
{
WorkflowBlueprint = workflowBlueprint,
WorkflowInstanceId = workflowInstance.Id,
ActivityType = bookmarkResult.ActivityTypeName ?? blockingActivity.Type,
ActivityId = blockingActivity.Id,
Bookmark = bookmarkResult.Bookmark
}));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,26 @@ public class TriggersForActivityBlueprintAndWorkflowProvider : IGetsTriggersForA
IDictionary<string, ActivityType> activityTypes,
CancellationToken cancellationToken = default)
{
var bookmarkProviderContext = GetBookmarkProviderContext(activityBlueprint, workflowExecutionContext, cancellationToken, activityTypes);
var supportedBookmarkProviders = await GetSupportedBookmarkProvidersForContextAsync(bookmarkProviderContext)
.ToListAsync(cancellationToken);
if (activityTypes.ContainsKey(activityBlueprint.Type))
{
var bookmarkProviderContext = GetBookmarkProviderContext(activityBlueprint, workflowExecutionContext, cancellationToken, activityTypes);
var supportedBookmarkProviders = await GetSupportedBookmarkProvidersForContextAsync(bookmarkProviderContext)
.ToListAsync(cancellationToken);

var tasksOfListsOfTriggers = supportedBookmarkProviders
.Select(bookmarkProvider => GetTriggersForBookmarkProvider(bookmarkProvider,
bookmarkProviderContext,
activityBlueprint,
workflowExecutionContext.WorkflowBlueprint,
cancellationToken));
return (await Task.WhenAll(tasksOfListsOfTriggers))
.SelectMany(x => x)
.ToList();
var tasksOfListsOfTriggers = supportedBookmarkProviders
.Select(bookmarkProvider => GetTriggersForBookmarkProvider(bookmarkProvider,
bookmarkProviderContext,
activityBlueprint,
workflowExecutionContext.WorkflowBlueprint,
cancellationToken));
return (await Task.WhenAll(tasksOfListsOfTriggers))
.SelectMany(x => x)
.ToList();
}
else
{
return Enumerable.Empty<WorkflowTrigger>();
}
}

private BookmarkProviderContext GetBookmarkProviderContext(
Expand Down
19 changes: 17 additions & 2 deletions src/core/Elsa.Core/Services/Workflows/ActivityTypeService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
Expand All @@ -11,6 +12,7 @@
using Elsa.Services.Models;
using MediatR;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;

namespace Elsa.Services.Workflows
{
Expand All @@ -22,12 +24,15 @@ public class ActivityTypeService : IActivityTypeService
private readonly IMediator _mediator;
private readonly ICacheSignal _cacheSignal;

public ActivityTypeService(IEnumerable<IActivityTypeProvider> providers, IMemoryCache memoryCache, IMediator mediator, ICacheSignal cacheSignal)
private readonly ILogger _logger;

public ActivityTypeService(IEnumerable<IActivityTypeProvider> providers, IMemoryCache memoryCache, IMediator mediator, ICacheSignal cacheSignal, ILogger<ActivityTypeService> logger)
{
_providers = providers;
_memoryCache = memoryCache;
_mediator = mediator;
_cacheSignal = cacheSignal;
_logger = logger;
}

public async ValueTask<IEnumerable<ActivityType>> GetActivityTypesAsync(CancellationToken cancellationToken) => (await GetDictionaryAsync(cancellationToken)).Values;
Expand Down Expand Up @@ -77,10 +82,20 @@ private async IAsyncEnumerable<ActivityType> GetActivityTypesInternalAsync([Enum
{
foreach (var provider in _providers)
{
var types = await provider.GetActivityTypesAsync(cancellationToken);
IEnumerable<ActivityType> types = Enumerable.Empty<ActivityType>();

try
{
types = await provider.GetActivityTypesAsync(cancellationToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occured while generating activities from provider {provider}", provider.GetType());
}

foreach (var type in types)
yield return type;

}
}
}
Expand Down
Loading