| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using Elsa.Alterations.Core.Contracts; |
| | | 4 | | using Elsa.Alterations.Core.Entities; |
| | | 5 | | using Elsa.Alterations.Core.Enums; |
| | | 6 | | using Elsa.Alterations.Core.Filters; |
| | | 7 | | using Elsa.Alterations.Core.Models; |
| | | 8 | | using Elsa.Common; |
| | | 9 | | using Elsa.Extensions; |
| | | 10 | | using Elsa.Workflows; |
| | | 11 | | using Elsa.Workflows.Attributes; |
| | | 12 | | using Elsa.Workflows.Exceptions; |
| | | 13 | | using Elsa.Workflows.Memory; |
| | | 14 | | using Elsa.Workflows.Models; |
| | | 15 | | |
| | | 16 | | namespace Elsa.Alterations.Activities; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Submits an alteration plan for execution. |
| | | 20 | | /// </summary> |
| | | 21 | | [Browsable(false)] |
| | | 22 | | [Activity("Elsa", "Alterations", "Generates jobs for the specified Alteration Plan", Kind = ActivityKind.Task)] |
| | | 23 | | public class GenerateAlterationJobs : CodeActivity<int> |
| | | 24 | | { |
| | | 25 | | /// <inheritdoc /> |
| | 0 | 26 | | public GenerateAlterationJobs([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(so |
| | | 27 | | { |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | 19 | 31 | | public GenerateAlterationJobs(Variable<string> planId, [CallerFilePath] string? source = null, [CallerLineNumber] in |
| | | 32 | | { |
| | 19 | 33 | | PlanId = new Input<string>(planId); |
| | 19 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// The ID of the submitted alteration plan. |
| | | 38 | | /// </summary> |
| | 67 | 39 | | public Input<string> PlanId { get; set; } = null!; |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 43 | | { |
| | 0 | 44 | | var plan = await GetPlanAsync(context); |
| | 0 | 45 | | await UpdatePlanStatusAsync(context, plan); |
| | 0 | 46 | | var workflowInstanceIds = (await FindMatchingWorkflowInstanceIdsAsync(context, plan.WorkflowInstanceFilter)).ToL |
| | | 47 | | |
| | 0 | 48 | | if (workflowInstanceIds.Any()) |
| | 0 | 49 | | await GenerateJobsAsync(context, plan, workflowInstanceIds); |
| | | 50 | | |
| | 0 | 51 | | context.SetResult(workflowInstanceIds.Count); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | private async Task<AlterationPlan> GetPlanAsync(ActivityExecutionContext context) |
| | | 55 | | { |
| | 0 | 56 | | var cancellationToken = context.CancellationToken; |
| | 0 | 57 | | var planId = context.Get(PlanId)!; |
| | 0 | 58 | | var alterationPlanStore = context.GetRequiredService<IAlterationPlanStore>(); |
| | 0 | 59 | | var planFilter = new AlterationPlanFilter |
| | 0 | 60 | | { |
| | 0 | 61 | | Id = planId |
| | 0 | 62 | | }; |
| | 0 | 63 | | var plan = await alterationPlanStore.FindAsync(planFilter, cancellationToken); |
| | | 64 | | |
| | 0 | 65 | | if (plan == null) |
| | 0 | 66 | | throw new FaultException(AlterationFaultCodes.PlanNotFound, AlterationFaultCategories.Alteration, DefaultFau |
| | | 67 | | |
| | 0 | 68 | | return plan; |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | private async Task UpdatePlanStatusAsync(ActivityExecutionContext context, AlterationPlan plan) |
| | | 72 | | { |
| | 0 | 73 | | var cancellationToken = context.CancellationToken; |
| | 0 | 74 | | var alterationPlanStore = context.GetRequiredService<IAlterationPlanStore>(); |
| | 0 | 75 | | plan.Status = AlterationPlanStatus.Generating; |
| | 0 | 76 | | await alterationPlanStore.SaveAsync(plan, cancellationToken); |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | private async Task<IEnumerable<string>> FindMatchingWorkflowInstanceIdsAsync(ActivityExecutionContext context, Alter |
| | | 80 | | { |
| | 0 | 81 | | var cancellationToken = context.CancellationToken; |
| | 0 | 82 | | var workflowInstanceFinder = context.GetRequiredService<IWorkflowInstanceFinder>(); |
| | 0 | 83 | | return await workflowInstanceFinder.FindAsync(filter, cancellationToken); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | private async Task GenerateJobsAsync(ActivityExecutionContext context, AlterationPlan plan, IEnumerable<string> work |
| | | 87 | | { |
| | 0 | 88 | | var cancellationToken = context.CancellationToken; |
| | 0 | 89 | | var identityGenerator = context.GetRequiredService<IIdentityGenerator>(); |
| | 0 | 90 | | var systemClock = context.GetRequiredService<ISystemClock>(); |
| | 0 | 91 | | var jobs = workflowInstanceIds.Select(workflowInstanceId => new AlterationJob |
| | 0 | 92 | | { |
| | 0 | 93 | | Id = identityGenerator.GenerateId(), |
| | 0 | 94 | | PlanId = plan.Id, |
| | 0 | 95 | | Status = AlterationJobStatus.Pending, |
| | 0 | 96 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 97 | | CreatedAt = systemClock.UtcNow |
| | 0 | 98 | | }) |
| | 0 | 99 | | .ToList(); |
| | | 100 | | |
| | 0 | 101 | | var alterationJobStore = context.GetRequiredService<IAlterationJobStore>(); |
| | 0 | 102 | | await alterationJobStore.SaveManyAsync(jobs, cancellationToken); |
| | 0 | 103 | | } |
| | | 104 | | } |