| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Common.Entities; |
| | | 3 | | using Elsa.Common.Models; |
| | | 4 | | using Elsa.Mediator.Contracts; |
| | | 5 | | using Elsa.Workflows.Activities; |
| | | 6 | | using Elsa.Workflows.Management.Entities; |
| | | 7 | | using Elsa.Workflows.Management.Filters; |
| | | 8 | | using Elsa.Workflows.Management.Materializers; |
| | | 9 | | using Elsa.Workflows.Management.Models; |
| | | 10 | | using Elsa.Workflows.Management.Notifications; |
| | | 11 | | using Elsa.Workflows.Models; |
| | | 12 | | |
| | | 13 | | namespace Elsa.Workflows.Management.Services; |
| | | 14 | | |
| | | 15 | | /// <inheritdoc /> |
| | 320 | 16 | | public class WorkflowDefinitionPublisher( |
| | 320 | 17 | | IWorkflowDefinitionService workflowDefinitionService, |
| | 320 | 18 | | IWorkflowDefinitionStore workflowDefinitionStore, |
| | 320 | 19 | | IWorkflowValidator workflowValidator, |
| | 320 | 20 | | IIdentityGenerator identityGenerator, |
| | 320 | 21 | | IActivitySerializer activitySerializer, |
| | 320 | 22 | | IMediator mediator, |
| | 320 | 23 | | ISystemClock systemClock) |
| | | 24 | | : IWorkflowDefinitionPublisher |
| | | 25 | | { |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public WorkflowDefinition New(IActivity? root = null) |
| | | 28 | | { |
| | 0 | 29 | | root ??= new Sequence(); |
| | 0 | 30 | | var id = identityGenerator.GenerateId(); |
| | 0 | 31 | | var definitionId = identityGenerator.GenerateId(); |
| | | 32 | | const int version = 1; |
| | | 33 | | |
| | 0 | 34 | | return new() |
| | 0 | 35 | | { |
| | 0 | 36 | | Id = id, |
| | 0 | 37 | | DefinitionId = definitionId, |
| | 0 | 38 | | Version = version, |
| | 0 | 39 | | IsLatest = true, |
| | 0 | 40 | | IsPublished = false, |
| | 0 | 41 | | CreatedAt = systemClock.UtcNow, |
| | 0 | 42 | | StringData = activitySerializer.Serialize(root), |
| | 0 | 43 | | MaterializerName = JsonWorkflowMaterializer.MaterializerName |
| | 0 | 44 | | }; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public Task<WorkflowDefinition> NewAsync(IActivity? root = null, CancellationToken cancellationToken = default) |
| | | 48 | | { |
| | 4 | 49 | | root ??= new Sequence(); |
| | 4 | 50 | | var id = identityGenerator.GenerateId(); |
| | 4 | 51 | | var definitionId = identityGenerator.GenerateId(); |
| | | 52 | | const int version = 1; |
| | | 53 | | |
| | 4 | 54 | | var workflowDefinition = new WorkflowDefinition |
| | 4 | 55 | | { |
| | 4 | 56 | | Id = id, |
| | 4 | 57 | | DefinitionId = definitionId, |
| | 4 | 58 | | Version = version, |
| | 4 | 59 | | IsLatest = true, |
| | 4 | 60 | | IsPublished = false, |
| | 4 | 61 | | CreatedAt = systemClock.UtcNow, |
| | 4 | 62 | | StringData = activitySerializer.Serialize(root), |
| | 4 | 63 | | MaterializerName = JsonWorkflowMaterializer.MaterializerName |
| | 4 | 64 | | }; |
| | | 65 | | |
| | 4 | 66 | | return Task.FromResult(workflowDefinition); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | public async Task<PublishWorkflowDefinitionResult> PublishAsync(string definitionId, CancellationToken cancellationT |
| | | 71 | | { |
| | 1 | 72 | | var filter = WorkflowDefinitionHandle.ByDefinitionId(definitionId, VersionOptions.Latest).ToFilter(); |
| | 1 | 73 | | var definition = await workflowDefinitionStore.FindAsync(filter, cancellationToken); |
| | | 74 | | |
| | 1 | 75 | | if (definition == null) |
| | 0 | 76 | | return new(false, new List<WorkflowValidationError> |
| | 0 | 77 | | { |
| | 0 | 78 | | new("Workflow definition not found.") |
| | 0 | 79 | | }, new([])); |
| | | 80 | | |
| | 1 | 81 | | return await PublishAsync(definition, cancellationToken); |
| | 1 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <inheritdoc /> |
| | | 85 | | public async Task<PublishWorkflowDefinitionResult> PublishAsync(WorkflowDefinition definition, CancellationToken can |
| | | 86 | | { |
| | 5 | 87 | | var workflowGraph = await workflowDefinitionService.MaterializeWorkflowAsync(definition, cancellationToken); |
| | 5 | 88 | | var validationErrors = (await workflowValidator.ValidateAsync(workflowGraph.Workflow, cancellationToken)).ToList |
| | | 89 | | |
| | 5 | 90 | | if (validationErrors.Any()) |
| | 0 | 91 | | return new(false, validationErrors, new([])); |
| | | 92 | | |
| | 5 | 93 | | await mediator.SendAsync(new WorkflowDefinitionPublishing(definition), cancellationToken); |
| | 5 | 94 | | var definitionId = definition.DefinitionId; |
| | | 95 | | |
| | | 96 | | // Reset current latest and published definitions. |
| | 5 | 97 | | var publishedWorkflows = await workflowDefinitionStore.FindManyAsync(new() |
| | 5 | 98 | | { |
| | 5 | 99 | | DefinitionId = definitionId, |
| | 5 | 100 | | VersionOptions = VersionOptions.LatestOrPublished |
| | 5 | 101 | | }, cancellationToken); |
| | | 102 | | |
| | 18 | 103 | | foreach (var publishedAndOrLatestWorkflow in publishedWorkflows) |
| | | 104 | | { |
| | 4 | 105 | | var isPublished = publishedAndOrLatestWorkflow.IsPublished; |
| | 4 | 106 | | publishedAndOrLatestWorkflow.IsPublished = false; |
| | 4 | 107 | | publishedAndOrLatestWorkflow.IsLatest = false; |
| | 4 | 108 | | await workflowDefinitionStore.SaveAsync(publishedAndOrLatestWorkflow, cancellationToken); |
| | | 109 | | |
| | 4 | 110 | | if (isPublished) |
| | 3 | 111 | | await mediator.SendAsync(new WorkflowDefinitionVersionRetracted(publishedAndOrLatestWorkflow), cancellat |
| | 4 | 112 | | } |
| | | 113 | | |
| | | 114 | | // Save the newly published definition. |
| | 5 | 115 | | definition.IsPublished = true; |
| | 5 | 116 | | definition.IsLatest = true; |
| | 5 | 117 | | definition = Initialize(definition); |
| | 5 | 118 | | await workflowDefinitionStore.SaveAsync(definition, cancellationToken); |
| | | 119 | | |
| | 5 | 120 | | var affectedWorkflows = new AffectedWorkflows(new List<WorkflowDefinition>()); |
| | 5 | 121 | | await mediator.SendAsync(new WorkflowDefinitionPublished(definition, affectedWorkflows), cancellationToken); |
| | 5 | 122 | | return new(true, validationErrors, affectedWorkflows); |
| | 5 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <inheritdoc /> |
| | | 126 | | public async Task<WorkflowDefinition?> RetractAsync(string definitionId, CancellationToken cancellationToken = defau |
| | | 127 | | { |
| | 0 | 128 | | var filter = WorkflowDefinitionHandle.ByDefinitionId(definitionId, VersionOptions.Published).ToFilter(); |
| | 0 | 129 | | var definition = await workflowDefinitionStore.FindAsync(filter, cancellationToken); |
| | | 130 | | |
| | 0 | 131 | | if (definition == null) |
| | 0 | 132 | | return null; |
| | | 133 | | |
| | 0 | 134 | | return await RetractAsync(definition, cancellationToken); |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <inheritdoc /> |
| | | 138 | | public async Task<WorkflowDefinition> RetractAsync(WorkflowDefinition definition, CancellationToken cancellationToke |
| | | 139 | | { |
| | 0 | 140 | | if (!definition.IsPublished) |
| | 0 | 141 | | throw new InvalidOperationException("Cannot retract an unpublished workflow definition."); |
| | | 142 | | |
| | 0 | 143 | | definition.IsPublished = false; |
| | | 144 | | |
| | 0 | 145 | | await mediator.SendAsync(new WorkflowDefinitionRetracting(definition), cancellationToken); |
| | 0 | 146 | | await workflowDefinitionStore.SaveAsync(definition, cancellationToken); |
| | 0 | 147 | | await mediator.SendAsync(new WorkflowDefinitionRetracted(definition), cancellationToken); |
| | 0 | 148 | | return definition; |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | public async Task<WorkflowDefinition> RevertVersionAsync(string definitionId, int version, CancellationToken cancell |
| | | 152 | | { |
| | 0 | 153 | | var filter = new WorkflowDefinitionFilter |
| | 0 | 154 | | { |
| | 0 | 155 | | DefinitionId = definitionId, |
| | 0 | 156 | | VersionOptions = VersionOptions.Latest |
| | 0 | 157 | | }; |
| | 0 | 158 | | var latestVersion = await workflowDefinitionStore.FindAsync(filter, cancellationToken); |
| | | 159 | | |
| | 0 | 160 | | if (latestVersion != null) |
| | | 161 | | { |
| | 0 | 162 | | latestVersion.IsLatest = false; |
| | 0 | 163 | | await workflowDefinitionStore.SaveAsync(latestVersion, cancellationToken); |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | var draft = await GetDraftAsync(definitionId, VersionOptions.SpecificVersion(version), cancellationToken); |
| | 0 | 167 | | draft!.Id = identityGenerator.GenerateId(); |
| | 0 | 168 | | draft.Version = (latestVersion?.Version ?? 0) + 1; |
| | 0 | 169 | | draft.IsLatest = true; |
| | | 170 | | |
| | 0 | 171 | | await workflowDefinitionStore.SaveAsync(draft, cancellationToken); |
| | 0 | 172 | | return draft; |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <inheritdoc /> |
| | | 176 | | public async Task<WorkflowDefinition?> GetDraftAsync(string definitionId, VersionOptions versionOptions, Cancellatio |
| | | 177 | | { |
| | 6 | 178 | | var filter = new WorkflowDefinitionFilter |
| | 6 | 179 | | { |
| | 6 | 180 | | DefinitionId = definitionId, |
| | 6 | 181 | | VersionOptions = versionOptions |
| | 6 | 182 | | }; |
| | 6 | 183 | | var order = new WorkflowDefinitionOrder<int>(x => x.Version, OrderDirection.Descending); |
| | 6 | 184 | | var lastVersion = await workflowDefinitionStore.FindLastVersionAsync(new() |
| | 6 | 185 | | { |
| | 6 | 186 | | DefinitionId = definitionId |
| | 6 | 187 | | }, cancellationToken); |
| | 6 | 188 | | var definition = await workflowDefinitionStore.FindAsync(filter, order, cancellationToken) ?? lastVersion; |
| | | 189 | | |
| | 6 | 190 | | if (definition == null!) |
| | 4 | 191 | | return null; |
| | | 192 | | |
| | 2 | 193 | | if (!definition.IsPublished) |
| | 0 | 194 | | return definition; |
| | | 195 | | |
| | 2 | 196 | | var draft = definition.ShallowClone(); |
| | | 197 | | |
| | 2 | 198 | | draft.Version = lastVersion?.Version + 1 ?? 1; |
| | 2 | 199 | | draft.CreatedAt = systemClock.UtcNow; |
| | 2 | 200 | | draft.Id = identityGenerator.GenerateId(); |
| | 2 | 201 | | draft.IsLatest = true; |
| | 2 | 202 | | draft.IsPublished = false; |
| | | 203 | | |
| | 2 | 204 | | return draft; |
| | 6 | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <inheritdoc /> |
| | | 208 | | public async Task<WorkflowDefinition> SaveDraftAsync(WorkflowDefinition definition, CancellationToken cancellationTo |
| | | 209 | | { |
| | 2 | 210 | | var draft = definition; |
| | 2 | 211 | | var definitionId = definition.DefinitionId; |
| | 2 | 212 | | var filter = new WorkflowDefinitionFilter |
| | 2 | 213 | | { |
| | 2 | 214 | | DefinitionId = definitionId |
| | 2 | 215 | | }; |
| | 2 | 216 | | var lastVersion = await workflowDefinitionStore.FindLastVersionAsync(filter, cancellationToken); |
| | | 217 | | |
| | 2 | 218 | | draft.Version = draft.Id == lastVersion?.Id ? lastVersion.Version : lastVersion?.Version + 1 ?? 1; |
| | 2 | 219 | | draft.IsLatest = true; |
| | 2 | 220 | | draft = Initialize(draft); |
| | | 221 | | |
| | 2 | 222 | | await mediator.SendAsync(new WorkflowDefinitionDraftSaving(draft), cancellationToken); |
| | 2 | 223 | | await workflowDefinitionStore.SaveAsync(draft, cancellationToken); |
| | 2 | 224 | | await mediator.SendAsync(new WorkflowDefinitionDraftSaved(draft), cancellationToken); |
| | | 225 | | |
| | 2 | 226 | | if (lastVersion is null) |
| | 2 | 227 | | await mediator.SendAsync(new WorkflowDefinitionCreated(definition), cancellationToken); |
| | | 228 | | |
| | 2 | 229 | | if (lastVersion is { IsPublished: true, IsLatest: true }) |
| | | 230 | | { |
| | 0 | 231 | | lastVersion.IsLatest = false; |
| | 0 | 232 | | await workflowDefinitionStore.SaveAsync(lastVersion, cancellationToken); |
| | | 233 | | } |
| | | 234 | | |
| | 2 | 235 | | return draft; |
| | 2 | 236 | | } |
| | | 237 | | |
| | | 238 | | private WorkflowDefinition Initialize(WorkflowDefinition definition) |
| | | 239 | | { |
| | 7 | 240 | | if (definition.Id == null!) |
| | 0 | 241 | | definition.Id = identityGenerator.GenerateId(); |
| | | 242 | | |
| | 7 | 243 | | if (definition.DefinitionId == null!) |
| | 0 | 244 | | definition.DefinitionId = identityGenerator.GenerateId(); |
| | | 245 | | |
| | 7 | 246 | | if (definition.Version == 0) |
| | 0 | 247 | | definition.Version = 1; |
| | | 248 | | |
| | 7 | 249 | | return definition; |
| | | 250 | | } |
| | | 251 | | } |