| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Workflows.Management.Materializers; |
| | | 4 | | using Elsa.Workflows.Runtime.Features; |
| | | 5 | | using Elsa.Workflows.Runtime.Options; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Runtime.Providers; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides workflows to the system that are registered with <see cref="WorkflowRuntimeFeature"/> |
| | | 13 | | /// </summary> |
| | | 14 | | [UsedImplicitly] |
| | 403 | 15 | | public class ClrWorkflowsProvider( |
| | 403 | 16 | | IOptions<RuntimeOptions> options, |
| | 403 | 17 | | IWorkflowBuilderFactory workflowBuilderFactory, |
| | 403 | 18 | | ITenantAccessor tenantAccessor, |
| | 403 | 19 | | IServiceProvider serviceProvider) : IWorkflowsProvider |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc /> |
| | 416 | 22 | | public string Name => "CLR"; |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public async ValueTask<IEnumerable<MaterializedWorkflow>> GetWorkflowsAsync(CancellationToken cancellationToken = de |
| | | 26 | | { |
| | 700 | 27 | | var buildWorkflowTasks = options.Value.Workflows.Values.Select(async x => await BuildWorkflowAsync(x, cancellati |
| | 284 | 28 | | var workflowDefinitions = await Task.WhenAll(buildWorkflowTasks); |
| | 284 | 29 | | return workflowDefinitions; |
| | 284 | 30 | | } |
| | | 31 | | |
| | | 32 | | private async Task<MaterializedWorkflow> BuildWorkflowAsync(Func<IServiceProvider, ValueTask<IWorkflow>> workflowFac |
| | | 33 | | { |
| | 416 | 34 | | var builder = workflowBuilderFactory.CreateBuilder(); |
| | 416 | 35 | | var workflowBuilder = await workflowFactory(serviceProvider); |
| | 416 | 36 | | var workflowBuilderType = workflowBuilder.GetType(); |
| | 416 | 37 | | var tenant = tenantAccessor.Tenant; |
| | 416 | 38 | | var tenantPrefix = !string.IsNullOrEmpty(tenant?.Id) ? $"{tenant.Id}:" : string.Empty; |
| | 416 | 39 | | await workflowBuilder.BuildAsync(builder, cancellationToken); |
| | 416 | 40 | | var workflow = await builder.BuildWorkflowAsync(cancellationToken); |
| | 416 | 41 | | var versionSuffix = $"v{workflow.Version}"; |
| | 416 | 42 | | var definitionId = string.IsNullOrEmpty(workflow.Identity.DefinitionId) ? tenantPrefix + workflowBuilderType.Nam |
| | 416 | 43 | | var id = string.IsNullOrEmpty(workflow.Identity.Id) ? $"{tenantPrefix}{workflowBuilderType.Name}:{versionSuffix} |
| | 416 | 44 | | var tenantId = string.IsNullOrEmpty(workflow.Identity.TenantId) ? tenant?.Id : workflow.Identity.TenantId; |
| | | 45 | | |
| | 416 | 46 | | workflow.Identity = workflow.Identity with |
| | 416 | 47 | | { |
| | 416 | 48 | | Id = id, |
| | 416 | 49 | | DefinitionId = definitionId, |
| | 416 | 50 | | TenantId = tenantId?.NullIfEmpty() |
| | 416 | 51 | | }; |
| | | 52 | | |
| | 416 | 53 | | var materializerContext = new ClrWorkflowMaterializerContext(workflowBuilder.GetType()); |
| | 416 | 54 | | return new(workflow, Name, ClrWorkflowMaterializer.MaterializerName, materializerContext); |
| | 416 | 55 | | } |
| | | 56 | | } |