| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Http.Bookmarks; |
| | | 3 | | using Elsa.Http.Contexts; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | using Elsa.Workflows.Runtime; |
| | | 7 | | using Elsa.Workflows.Runtime.Entities; |
| | | 8 | | using Elsa.Workflows.Runtime.Filters; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Http.Services; |
| | | 11 | | |
| | | 12 | | /// <inheritdoc /> |
| | 323 | 13 | | public class DefaultRouteTableUpdater( |
| | 323 | 14 | | IRouteTable routeTable, |
| | 323 | 15 | | ITriggerStore triggerStore, |
| | 323 | 16 | | IBookmarkStore bookmarkStore, |
| | 323 | 17 | | IHttpEndpointRoutesProvider httpEndpointRoutesProvider) |
| | | 18 | | : IRouteTableUpdater |
| | | 19 | | { |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public async Task UpdateAsync(CancellationToken cancellationToken = default) |
| | | 22 | | { |
| | 1 | 23 | | var stimulusName = HttpStimulusNames.HttpEndpoint; |
| | 1 | 24 | | var triggerFilter = new TriggerFilter |
| | 1 | 25 | | { |
| | 1 | 26 | | Name = stimulusName |
| | 1 | 27 | | }; |
| | 1 | 28 | | var bookmarkFilter = new BookmarkFilter |
| | 1 | 29 | | { |
| | 1 | 30 | | Name = stimulusName |
| | 1 | 31 | | }; |
| | 1 | 32 | | var triggers = (await triggerStore.FindManyAsync(triggerFilter, cancellationToken)).ToList(); |
| | 1 | 33 | | var bookmarks = (await bookmarkStore.FindManyAsync(bookmarkFilter, cancellationToken)).ToList(); |
| | | 34 | | |
| | 1 | 35 | | await AddRoutesAsync(triggers, cancellationToken); |
| | 1 | 36 | | await AddRoutesAsync(bookmarks, cancellationToken); |
| | 1 | 37 | | } |
| | | 38 | | |
| | | 39 | | public async Task AddRoutesAsync(IEnumerable<StoredTrigger> triggers, CancellationToken cancellationToken = default) |
| | | 40 | | { |
| | 985 | 41 | | var httpEndpointTriggers = Filter(triggers).ToList(); |
| | | 42 | | |
| | 2082 | 43 | | foreach (var trigger in httpEndpointTriggers) |
| | | 44 | | { |
| | 56 | 45 | | var payload = trigger.GetPayload<HttpEndpointBookmarkPayload>(); |
| | 56 | 46 | | await AddRoutesAsync(payload, trigger.TenantId, cancellationToken); |
| | | 47 | | } |
| | 985 | 48 | | } |
| | | 49 | | |
| | | 50 | | public async Task AddRoutesAsync(IEnumerable<StoredBookmark> bookmarks, CancellationToken cancellationToken = defaul |
| | | 51 | | { |
| | 1 | 52 | | var httpEndpointBookmarks = Filter(bookmarks).ToList(); |
| | | 53 | | |
| | 2 | 54 | | foreach (var bookmark in httpEndpointBookmarks) |
| | | 55 | | { |
| | 0 | 56 | | var payload = bookmark.GetPayload<HttpEndpointBookmarkPayload>(); |
| | 0 | 57 | | await AddRoutesAsync(payload, bookmark.TenantId, cancellationToken); |
| | | 58 | | } |
| | 1 | 59 | | } |
| | | 60 | | |
| | | 61 | | public async Task AddRoutesAsync(IEnumerable<Bookmark> bookmarks, WorkflowExecutionContext workflowExecutionContext, |
| | | 62 | | { |
| | 325 | 63 | | var httpEndpointBookmarks = Filter(bookmarks).ToList(); |
| | | 64 | | |
| | 662 | 65 | | foreach (var bookmark in httpEndpointBookmarks) |
| | | 66 | | { |
| | 6 | 67 | | var payload = bookmark.GetPayload<HttpEndpointBookmarkPayload>(); |
| | 6 | 68 | | await AddRoutesAsync(payload, workflowExecutionContext.Workflow.Identity.TenantId, cancellationToken); |
| | | 69 | | } |
| | 325 | 70 | | } |
| | | 71 | | |
| | | 72 | | public void RemoveRoutes(IEnumerable<StoredTrigger> triggers) |
| | | 73 | | { |
| | 496 | 74 | | var paths = Filter(triggers).Select(x => x.GetPayload<HttpEndpointBookmarkPayload>().Path).ToList(); |
| | 492 | 75 | | routeTable.RemoveRange(paths); |
| | 492 | 76 | | } |
| | | 77 | | |
| | | 78 | | public void RemoveRoutes(IEnumerable<Bookmark> bookmarks) |
| | | 79 | | { |
| | 327 | 80 | | var paths = Filter(bookmarks).Select(x => x.GetPayload<HttpEndpointBookmarkPayload>().Path).ToList(); |
| | 325 | 81 | | routeTable.RemoveRange(paths); |
| | 325 | 82 | | } |
| | | 83 | | |
| | | 84 | | private async Task AddRoutesAsync(HttpEndpointBookmarkPayload payload, string? tenantId, CancellationToken cancellat |
| | | 85 | | { |
| | 62 | 86 | | var context = new HttpEndpointRouteProviderContext(payload, tenantId, cancellationToken); |
| | 62 | 87 | | var routes = await httpEndpointRoutesProvider.GetRoutesAsync(context); |
| | | 88 | | |
| | 248 | 89 | | foreach (var route in routes) |
| | 62 | 90 | | routeTable.Add(route); |
| | 62 | 91 | | } |
| | | 92 | | |
| | | 93 | | private static IEnumerable<StoredTrigger> Filter(IEnumerable<StoredTrigger> triggers) |
| | | 94 | | { |
| | 1477 | 95 | | var stimulusName = HttpStimulusNames.HttpEndpoint; |
| | 1551 | 96 | | return triggers.Where(x => x.Name == stimulusName && x.Payload != null); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private static IEnumerable<StoredBookmark> Filter(IEnumerable<StoredBookmark> bookmarks) |
| | | 100 | | { |
| | 1 | 101 | | var stimulusName = HttpStimulusNames.HttpEndpoint; |
| | 1 | 102 | | return bookmarks.Where(x => x.Name == stimulusName && x.Payload != null); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | private static IEnumerable<Bookmark> Filter(IEnumerable<Bookmark> bookmarks) |
| | | 106 | | { |
| | 650 | 107 | | var stimulusName = HttpStimulusNames.HttpEndpoint; |
| | 714 | 108 | | return bookmarks.Where(x => x.Name == stimulusName && x.Payload != null); |
| | | 109 | | } |
| | | 110 | | } |