| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using Elsa.Alterations.Core.Contracts; |
| | | 4 | | using Elsa.Alterations.Core.Enums; |
| | | 5 | | using Elsa.Alterations.Core.Filters; |
| | | 6 | | using Elsa.Workflows; |
| | | 7 | | using Elsa.Workflows.Attributes; |
| | | 8 | | using Elsa.Workflows.Exceptions; |
| | | 9 | | using Elsa.Workflows.Memory; |
| | | 10 | | using Elsa.Workflows.Models; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Alterations.Activities; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Submits an alteration plan for execution. |
| | | 16 | | /// </summary> |
| | | 17 | | [Browsable(false)] |
| | | 18 | | [Activity("Elsa", "Alterations", "Dispatches jobs for the specified Alteration Plan", Kind = ActivityKind.Task)] |
| | | 19 | | public class DispatchAlterationJobs : CodeActivity |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc /> |
| | 19 | 22 | | public DispatchAlterationJobs(Variable<string> planId, [CallerFilePath] string? source = null, [CallerLineNumber] in |
| | | 23 | | { |
| | 19 | 24 | | PlanId = new Input<string>(planId); |
| | 19 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | 0 | 28 | | public DispatchAlterationJobs([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(so |
| | | 29 | | { |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// The ID of the alteration plan. |
| | | 34 | | /// </summary> |
| | 67 | 35 | | public Input<string> PlanId { get; set; } = null!; |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 39 | | { |
| | 0 | 40 | | var cancellationToken = context.CancellationToken; |
| | 0 | 41 | | var planId = context.Get(PlanId)!; |
| | 0 | 42 | | var alterationPlanStore = context.GetRequiredService<IAlterationPlanStore>(); |
| | 0 | 43 | | var planFilter = new AlterationPlanFilter |
| | 0 | 44 | | { |
| | 0 | 45 | | Id = planId |
| | 0 | 46 | | }; |
| | 0 | 47 | | var plan = await alterationPlanStore.FindAsync(planFilter, cancellationToken); |
| | | 48 | | |
| | 0 | 49 | | if (plan == null) |
| | 0 | 50 | | throw new FaultException(AlterationFaultCodes.PlanNotFound, AlterationFaultCategories.Alteration, DefaultFau |
| | | 51 | | |
| | | 52 | | // Update status. |
| | 0 | 53 | | plan.Status = AlterationPlanStatus.Dispatching; |
| | 0 | 54 | | await alterationPlanStore.SaveAsync(plan, cancellationToken); |
| | | 55 | | |
| | | 56 | | // Find all jobs for the plan and dispatch them. |
| | 0 | 57 | | var filter = new AlterationJobFilter |
| | 0 | 58 | | { |
| | 0 | 59 | | PlanId = plan.Id |
| | 0 | 60 | | }; |
| | 0 | 61 | | var alterationJobStore = context.GetRequiredService<IAlterationJobStore>(); |
| | 0 | 62 | | var alterationJobIds = await alterationJobStore.FindManyIdsAsync(filter, cancellationToken); |
| | | 63 | | |
| | | 64 | | // Dispatch each job. |
| | 0 | 65 | | var alterationJobDispatcher = context.GetRequiredService<IAlterationJobDispatcher>(); |
| | 0 | 66 | | foreach (var jobId in alterationJobIds) |
| | 0 | 67 | | await alterationJobDispatcher.DispatchAsync(jobId, cancellationToken); |
| | | 68 | | |
| | | 69 | | // Update status. |
| | 0 | 70 | | plan.Status = AlterationPlanStatus.Running; |
| | 0 | 71 | | await alterationPlanStore.SaveAsync(plan, cancellationToken); |
| | 0 | 72 | | } |
| | | 73 | | } |